from database.db import db


class CarModel(db.Model):
    """
    Stores car model
    """
    __tablename__ = "car_model"

    model_id = db.Column(db.Integer, primary_key=True, nullable=False)
    model_name = db.Column(db.String(300), nullable=False)
    series = db.Column(db.String(100), nullable=False)
    make = db.Column(db.Integer, db.ForeignKey('car_make.make_id', ondelete='CASCADE', onupdate='CASCADE'))
    # describe the relationship with the vehicle_details of a policy holder
    vehicle_details = db.relationship("VehicleDetails", backref="car_model", lazy='dynamic')

    def __init__(self, model_name, series, make):
        self. model_name = model_name
        self.series = series
        self.make = make

    def save(self):
        db.session.add(self)
        db.session.commit()
    
    def serialize(self):
        return {
            "model_id": self.model_id,
            "model_name": self.model_name,
            "series": self.series
        }
    
    @classmethod
    def get_models_by_make_id(cls, make_id):
        car_models = cls.query.filter_by(make=make_id)
        return car_models
    
    @classmethod
    def get_model_name_by_id(cls, model_id):
        model = cls.query.filter_by(model_id=model_id).first()
        return model.model_name
