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
from userprofile.models import UserProfile

# Create mock data for Users and UserProfiles
users_data = [
    {'username': 'driver1', 'password': 'password123', 'email': 'driver1@example.com', 'phone_number': '1234567890', 'address': '123 Main St', 'date_of_birth': '1990-01-01', 'idcard_no': '123456789', 'geolocation': '12.34,56.78', 'business_type': 'Logistics'},
    {'username': 'driver2', 'password': 'password123', 'email': 'driver2@example.com', 'phone_number': '0987654321', 'address': '456 Elm St', 'date_of_birth': '1985-05-05', 'idcard_no': '987654321', 'geolocation': '98.76,54.32', 'business_type': 'Transport'},
]

users = []
for data in users_data:
    user, created = User.objects.get_or_create(username=data['username'], defaults={'email': data['email'], 'password': data['password']})
    if created:
        user.set_password(data['password'])  # Ensure the password is set correctly
        user.save()
    user_profile, created = UserProfile.objects.get_or_create(user=user, defaults={
        'phone_number': data['phone_number'],
        'address': data['address'],
        'date_of_birth': data['date_of_birth'],
        'idcard_no': data['idcard_no'],
        'geolocation': data['geolocation'],
        'business_type': data['business_type']
    })
    users.append(user)

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

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

# 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.objects.create(**data) for data in trip_data]

# 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.objects.create(**data) for data in maintenance_data]

# 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.objects.create(**data) for data in appointment_data]

# 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.objects.create(**data) for data in gps_data]

# 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.objects.create(**data) for data in fuel_transaction_data]

print("Mock data created successfully!")

