from django.db import models

from apps.vehicle.managers.vehicle_managers import (
    VehicleMakeManager,
    VehicleModelMasterManager,
    VehicleInsuranceManager,
)
from base.documents_path import DocumentsPath
from base.models import BaseModel


class VehicleMakeMaster(BaseModel):
    """
    This is the VehicleMakeMaster model.
    It represents system vehicle makers.

    Attributes:
        name (CharField): The name of the vehicle maker.
        logo (FileField): The logo associated with the vehicle maker.

    Meta:
        db_table (str): The database table name for the VehicleMakeMaster model.
    """

    name = models.CharField(null=False, blank=False, unique=True)
    logo = models.FileField(upload_to=DocumentsPath.get_vehicle_make_path, null=True)

    objects = VehicleMakeManager()

    class Meta:
        db_table = "vehicle_make_master"

    def __str__(self):
        return f"{self.name}"


class VehicleModelMaster(BaseModel):
    """
    This is the VehicleModel model.
    It represents the system vehicle models of a specific vehicle maker.

    Attributes:
        name (CharField): The name of the vehicle model.
        vehicle_make (ForeignKey): A foreign key reference to the associated VehicleMakeMaster.

    Meta:
        db_table (str): The database table name for the VehicleModel model.
    """

    name = models.CharField(null=False, blank=False)
    vehicle_make = models.ForeignKey(
        VehicleMakeMaster, on_delete=models.CASCADE, related_name="vehicle_model_makes"
    )
    connector = models.ForeignKey(
        "master.ConnectorMaster",
        on_delete=models.CASCADE,
        related_name="vehicle_model_connectors",
        null=True,
    )

    objects = VehicleModelMasterManager()

    class Meta:
        db_table = "vehicle_model_master"

    def __str__(self):
        return f"{self.name}"


class VehicleInsurance(BaseModel):
    """
    Model class for representing vehicle insurances.

    This model represents VehicleInsurance instances with fields for 'user_vehicle',
    'insurance_company', 'insurance_type', 'user', 'start_date', 'end_date', and 'policy'.

    Attributes:
        user_vehicle: ForeignKey to the UserVehicle model, establishing a relationship.
        insurance_company: ForeignKey to the InsuranceCompanyMaster model, establishing a relationship.
        insurance_type: ForeignKey to the InsuranceTypeMaster model, establishing a relationship.
        user: ForeignKey to the User model, establishing a relationship.
        start_date: The start date of the insurance coverage.
        end_date: The end date of the insurance coverage.
        policy: FileField for storing the insurance policy file.
        objects: Custom manager for querying VehicleInsurance instances.
    """

    user_vehicle = models.ForeignKey(
        "account.UserVehicle",
        on_delete=models.CASCADE,
        related_name="user_vehicle_insurances",
    )
    insurance_company = models.ForeignKey(
        "master.InsuranceCompanyMaster",
        on_delete=models.CASCADE,
        related_name="insurance_company_insurances",
    )
    insurance_types = models.ManyToManyField(
        "master.InsuranceTypeMaster",
        related_name="insurance_type_insurances",
    )
    user = models.ForeignKey(
        "account.User",
        on_delete=models.CASCADE,
        related_name="user_insurances",
    )
    start_date = models.DateField()
    end_date = models.DateField()
    policy = models.FileField(upload_to=DocumentsPath.get_vehicle_insurance_path)

    objects = VehicleInsuranceManager()

    class Meta:
        db_table = "vehicle_insurance"
