from django.db import models
from django.contrib.auth.models import User

class Vehicle(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='vehicles', default=1)
    make = models.CharField(max_length=255)
    model = models.CharField(max_length=255)
    year = models.IntegerField()
    license_plate = models.CharField(max_length=100, unique=True)
    capacity = models.IntegerField()
    status = models.CharField(max_length=50)  # e.g., available, in use, under maintenance

    def __str__(self):
        return f'{self.make} {self.model} ({self.year}) - {self.license_plate}'

class Trip(models.Model):
    vehicle = models.ForeignKey(Vehicle, related_name='trips', on_delete=models.CASCADE)
    driver = models.ForeignKey(User, related_name='trips', on_delete=models.CASCADE)
    start_location = models.CharField(max_length=255)
    end_location = models.CharField(max_length=255)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    mileage = models.FloatField()
    fuel_consumption = models.FloatField()

class MaintenanceLog(models.Model):
    vehicle = models.ForeignKey(Vehicle, related_name='maintenance_logs', on_delete=models.CASCADE)
    date = models.DateTimeField()
    description = models.TextField()
    mileage = models.FloatField()

class Appointment(models.Model):
    vehicle = models.ForeignKey(Vehicle, related_name='appointments', on_delete=models.CASCADE)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()
    purpose = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class GPSData(models.Model):
    vehicle = models.ForeignKey(Vehicle, related_name='gps_data', on_delete=models.CASCADE)
    latitude = models.FloatField()
    longitude = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)

class FuelTransaction(models.Model):
    vehicle = models.ForeignKey(Vehicle, related_name='fuel_transactions', on_delete=models.CASCADE)
    date = models.DateTimeField()
    amount = models.FloatField()
    price_per_liter = models.FloatField()
    total_cost = models.FloatField()
    fuel_card_number = models.CharField(max_length=100)
