import os
import django
import random
from datetime import datetime, timedelta

# Set up Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'recycle.settings')
django.setup()

from tracking.models import Vehicle, Trip, MaintenanceLog, Appointment, GPSData, FuelTransaction
from django.contrib.auth.models import User

# Create mock data for Vehicles
vehicle_data = [
    {'make': 'Toyota', 'model': 'Hilux', 'year': 2018, 'license_plate': 'ABC1234', 'capacity': 1000, 'status': 'available'},
    {'make': 'Ford', 'model': 'Ranger', 'year': 2019, 'license_plate': 'XYZ5678', 'capacity': 1200, 'status': 'in use'},
    {'make': 'Isuzu', 'model': 'D-Max', 'year': 2020, 'license_plate': 'LMN9101', 'capacity': 1500, 'status': 'under maintenance'},
]

vehicles = [Vehicle(**data) for data in vehicle_data]
Vehicle.objects.bulk_create(vehicles)

# Create mock data for Users
users = [
    User.objects.create_user(username='driver1', password='password123'),
    User.objects.create_user(username='driver2', password='password123'),
]

# Create mock data for Trips
trip_data = [
    {'vehicle': vehicles[0], 'driver': users[0], 'start_location': 'Location A', 'end_location': 'Location B', 'start_time': datetime.now() - timedelta(days=1), 'end_time': datetime.now(), 'mileage': 100, 'fuel_consumption': 10},
    {'vehicle': vehicles[1], 'driver': users[1], 'start_location': 'Location C', 'end_location': 'Location D', 'start_time': datetime.now() - timedelta(days=2), 'end_time': datetime.now() - timedelta(days=1), 'mileage': 150, 'fuel_consumption': 15},
]

trips = [Trip(**data) for data in trip_data]
Trip.objects.bulk_create(trips)

# Create mock data for MaintenanceLogs
maintenance_data = [
    {'vehicle': vehicles[2], 'date': datetime.now() - timedelta(days=10), 'description': 'Engine oil change', 'mileage': 20000},
    {'vehicle': vehicles[0], 'date': datetime.now() - timedelta(days=20), 'description': 'Brake pad replacement', 'mileage': 30000},
]

maintenance_logs = [MaintenanceLog(**data) for data in maintenance_data]
MaintenanceLog.objects.bulk_create(maintenance_logs)

# Create mock data for Appointments
appointment_data = [
    {'vehicle': vehicles[0], 'start_time': datetime.now() + timedelta(days=1), 'end_time': datetime.now() + timedelta(days=1, hours=2), 'purpose': 'Pickup recycling materials'},
    {'vehicle': vehicles[1], 'start_time': datetime.now() + timedelta(days=2), 'end_time': datetime.now() + timedelta(days=2, hours=3), 'purpose': 'Deliver recycled goods'},
]

appointments = [Appointment(**data) for data in appointment_data]
Appointment.objects.bulk_create(appointments)

# Create mock data for GPSData
gps_data = [
    {'vehicle': vehicles[0], 'latitude': 13.7563, 'longitude': 100.5018},
    {'vehicle': vehicles[1], 'latitude': 13.736717, 'longitude': 100.523186},
]

gps_records = [GPSData(**data) for data in gps_data]
GPSData.objects.bulk_create(gps_records)

# Create mock data for FuelTransactions
fuel_transaction_data = [
    {'vehicle': vehicles[0], 'date': datetime.now() - timedelta(days=1), 'amount': 50, 'price_per_liter': 1.2, 'total_cost': 60, 'fuel_card_number': '1234567890'},
    {'vehicle': vehicles[1], 'date': datetime.now() - timedelta(days=2), 'amount': 60, 'price_per_liter': 1.3, 'total_cost': 78, 'fuel_card_number': '0987654321'},
]

fuel_transactions = [FuelTransaction(**data) for data in fuel_transaction_data]
FuelTransaction.objects.bulk_create(fuel_transactions)

print("Mock data created successfully!")
