from django.core.management.base import BaseCommand
from django.utils import timezone
from recycle_core.models import (
    Role, Permission, RolePermission, User, Customer, Material, MaterialDetail, 
    Operation, Regulation, ComplianceCheck, Report, AuditLog, Authority, 
    SustainabilityTracking, SupportTicket, OperationType, ComplianceStatus, ReportType
)
import random
from faker import Faker

fake = Faker()

class Command(BaseCommand):
    help = 'Create mockup data for the project models'

    def handle(self, *args, **kwargs):
        self.create_roles()
        self.create_permissions()
        self.create_users()
        self.create_authorities()
        self.create_customers()
        self.create_materials()
        self.create_operations()
        self.create_regulations()
        self.create_compliance_checks()
        self.create_reports()
        self.create_audit_logs()
        self.create_sustainability_trackings()
        self.create_support_tickets()

    def create_roles(self):
        roles = ['Admin', 'Manager', 'Employee']
        for role_name in roles:
            Role.objects.create(role_name=role_name)

    def create_permissions(self):
        permissions = ['add_user', 'change_user', 'delete_user', 'view_user']
        for permission_name in permissions:
            Permission.objects.create(permission_name=permission_name)

    def create_users(self):
        roles = Role.objects.all()
        for _ in range(10):
            role = random.choice(roles)
            User.objects.create(
                username=fake.user_name(),
                password_hash=fake.password(),
                email=fake.email(),
                role=role,
                created_at=timezone.now()
            )

    def create_customers(self):
        for _ in range(10):
            Customer.objects.create(
                name=fake.company(),
                contact_info=fake.address()
            )

    def create_materials(self):
        materials = [
            ('Metal', 'Ferrous - Iron'),
            ('Metal', 'Ferrous - Steel'),
            ('Plastic', 'PET'),
            ('Plastic', 'HDPE'),
            ('Glass', 'Container - Bottles'),
            ('Glass', 'Flat - Windows'),
            ('Paper', 'Cardboard'),
            ('Electronic Waste', 'Computers'),
            ('Organic Waste', 'Food Waste'),
            ('Textile', 'Natural - Cotton')
        ]
        for material_name, subtype in materials:
            material = Material.objects.create(
                material_name=material_name,
                description=fake.text(),
                created_at=timezone.now(),
                updated_at=timezone.now()
            )
            MaterialDetail.objects.create(
                material=material,
                subtype=subtype
            )

    def create_operations(self):
        materials = Material.objects.all()
        customers = Customer.objects.all()
        for _ in range(20):
            material = random.choice(materials)
            customer = random.choice(customers)
            Operation.objects.create(
                material=material,
                customer=customer,
                operation_type=random.choice(OperationType.choices)[0],
                quantity=random.uniform(1, 100),
                operation_date=timezone.now(),
                created_at=timezone.now(),
                updated_at=timezone.now()
            )

    def create_regulations(self):
        regulations = [
            'Environmental Protection Act',
            'Waste Management Act',
            'Recycling and Reuse Guidelines',
            'Hazardous Waste Regulations',
            'Air Quality Standards',
            'Water Quality Standards',
            'Occupational Health and Safety Regulations'
        ]
        authorities = Authority.objects.all()
        for regulation_name in regulations:
            authority = random.choice(authorities)
            Regulation.objects.create(
                regulation_name=regulation_name,
                description=fake.text(),
                authority=authority,
                created_at=timezone.now(),
                updated_at=timezone.now()
            )

    def create_compliance_checks(self):
        regulations = Regulation.objects.all()
        users = User.objects.all()
        operations = Operation.objects.all()
        for _ in range(20):
            regulation = random.choice(regulations)
            user = random.choice(users)
            operation = random.choice(operations)
            ComplianceCheck.objects.create(
                regulation=regulation,
                user=user,
                operation=operation,
                status=random.choice(ComplianceStatus.choices)[0],
                check_date=timezone.now(),
                remarks=fake.text()
            )

    def create_reports(self):
        users = User.objects.all()
        for _ in range(10):
            user = random.choice(users)
            Report.objects.create(
                report_name=random.choice(ReportType.choices)[0],
                user=user,
                report_data=fake.text(),
                created_at=timezone.now()
            )

    def create_audit_logs(self):
        users = User.objects.all()
        for _ in range(20):
            user = random.choice(users)
            AuditLog.objects.create(
                user=user,
                action=fake.word(),
                action_details=fake.text(),
                action_time=timezone.now()
            )

    def create_authorities(self):
        authorities = [
            'Environmental Protection Agency (EPA)',
            'Local Government',
            'Health and Safety Authority',
            'Waste Management Authority',
            'Recycling Compliance Board',
            'Sustainability Council'
        ]
        for authority_name in authorities:
            Authority.objects.create(
                authority_name=authority_name,
                contact_info=fake.address()
            )

    def create_sustainability_trackings(self):
        operations = Operation.objects.all()
        for _ in range(10):
            operation = random.choice(operations)
            SustainabilityTracking.objects.create(
                operation=operation,
                carbon_footprint=random.uniform(1, 50),
                sustainability_measures=fake.text()
            )

    def create_support_tickets(self):
        users = User.objects.all()
        customers = Customer.objects.all()
        for _ in range(10):
            user = random.choice(users)
            customer = random.choice(customers)
            SupportTicket.objects.create(
                user=user,
                customer=customer,
                issue=fake.text(),
                created_at=timezone.now(),
                resolution=fake.text()
            )
