from .db import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin

class User(db.Model, UserMixin):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(40), nullable=False, unique = True)
    email = db.Column(db.String(255), nullable=False, unique = True)
    img_url = db.Column(db.String(100), nullable=False, default="https://review-a-loo-profile-image.s3.us-east-2.amazonaws.com/no-user.jpg")
    bio = db.Column(db.String(255))
    hashed_password = db.Column(db.String(255), nullable=False)

    @property
    def password(self):
        return self.hashed_password

    @password.setter
    def password(self, password):
        self.hashed_password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)
    
    def to_dict(self):
        return {
            "id": self.id,
            "username": self.username,
            "email": self.email,
            "img_url": self.img_url,
            "bio": self.bio
        }

    def to_dict_reviews(self):
        return {
            "id": self.id,
            "username": self.username,
            "email": self.email,
            "img_url": self.img_url,
            "bio": self.bio,
            "reviews": [review.to_dict() for review in self.reviews]
        }