from django.db import models
# Create your models here.

# Enumerations
class MaterialType(models.TextChoices):
    METAL = 'Metal'
    PLASTIC = 'Plastic'
    GLASS = 'Glass'
    PAPER = 'Paper'
    ELECTRONIC_WASTE = 'Electronic Waste'
    ORGANIC_WASTE = 'Organic Waste'
    TEXTILE = 'Textile'

class MetalSubType(models.TextChoices):
    FERROUS_IRON = 'Ferrous - Iron'
    FERROUS_STEEL = 'Ferrous - Steel'
    NONFERROUS_ALUMINUM = 'Non-Ferrous - Aluminum'
    NONFERROUS_COPPER = 'Non-Ferrous - Copper'
    NONFERROUS_BRASS = 'Non-Ferrous - Brass'

class PlasticSubType(models.TextChoices):
    PET = 'PET'
    HDPE = 'HDPE'
    PVC = 'PVC'
    LDPE = 'LDPE'
    PP = 'PP'
    PS = 'PS'

class GlassSubType(models.TextChoices):
    CONTAINER_BOTTLES = 'Container - Bottles'
    CONTAINER_JARS = 'Container - Jars'
    FLAT_WINDOWS = 'Flat - Windows'
    FLAT_MIRRORS = 'Flat - Mirrors'

class PaperSubType(models.TextChoices):
    CARDBOARD = 'Cardboard'
    OFFICE_PAPER = 'Office Paper'
    NEWSPAPER = 'Newspaper'
    MAGAZINES = 'Magazines'

class EWasteSubType(models.TextChoices):
    COMPUTERS = 'Computers'
    MOBILE_PHONES = 'Mobile Phones'
    TELEVISIONS = 'Televisions'
    SMALL_APPLIANCES = 'Small Appliances'

class OrganicWasteSubType(models.TextChoices):
    FOOD_WASTE = 'Food Waste'
    YARD_WASTE = 'Yard Waste'
    AGRICULTURAL_WASTE = 'Agricultural Waste'

class TextileSubType(models.TextChoices):
    NATURAL_COTTON = 'Natural - Cotton'
    NATURAL_WOOL = 'Natural - Wool'
    SYNTHETIC_POLYESTER = 'Synthetic - Polyester'
    SYNTHETIC_NYLON = 'Synthetic - Nylon'

class OperationType(models.TextChoices):
    COLLECTION = 'Collection'
    SORTING = 'Sorting'
    PROCESSING = 'Processing'
    SHREDDING = 'Shredding'
    MELTING = 'Melting'
    GRANULATING = 'Granulating'
    PACKAGING = 'Packaging'
    DISTRIBUTION = 'Distribution'

class ReportType(models.TextChoices):
    COLLECTION = 'Collection Report'
    PROCESSING = 'Processing Report'
    EFFICIENCY = 'Recycling Efficiency Report'
    COMPLIANCE = 'Compliance Report'
    SUSTAINABILITY = 'Sustainability Report'
    FINANCIAL = 'Financial Report'
    INVENTORY = 'Inventory Report'

class RegulationType(models.TextChoices):
    ENVIRONMENTAL_PROTECTION = 'Environmental Protection Act'
    WASTE_MANAGEMENT = 'Waste Management Act'
    RECYCLING_GUIDELINES = 'Recycling and Reuse Guidelines'
    HAZARDOUS_WASTE = 'Hazardous Waste Regulations'
    AIR_QUALITY = 'Air Quality Standards'
    WATER_QUALITY = 'Water Quality Standards'
    HEALTH_SAFETY = 'Occupational Health and Safety Regulations'

class ComplianceStatus(models.TextChoices):
    PENDING = 'Pending'
    IN_PROGRESS = 'In Progress'
    COMPLETED = 'Completed'
    FAILED = 'Failed'
    NOT_APPLICABLE = 'Not Applicable'

class AuthorityType(models.TextChoices):
    EPA = 'Environmental Protection Agency (EPA)'
    LOCAL_GOVERNMENT = 'Local Government'
    HEALTH_SAFETY = 'Health and Safety Authority'
    WASTE_MANAGEMENT = 'Waste Management Authority'
    RECYCLING_BOARD = 'Recycling Compliance Board'
    SUSTAINABILITY_COUNCIL = 'Sustainability Council'

# Database Models
class Role(models.Model):
    role_name = models.CharField(max_length=100)

class Permission(models.Model):
    permission_name = models.CharField(max_length=100)

class RolePermission(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    permission = models.ForeignKey(Permission, on_delete=models.CASCADE)

class User(models.Model):
    username = models.CharField(max_length=100)
    password_hash = models.CharField(max_length=255)
    email = models.EmailField(unique=True)
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

class Customer(models.Model):
    name = models.CharField(max_length=100)
    contact_info = models.TextField()
    email = models.EmailField(blank=True, null=True)
    phone_number = models.CharField(max_length=15, blank=True)

class Material(models.Model):
    material_name = models.CharField(max_length=100, choices=MaterialType.choices)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class MaterialDetail(models.Model):
    material = models.ForeignKey(Material, on_delete=models.CASCADE)
    subtype = models.CharField(max_length=100)

class Operation(models.Model):
    material = models.ForeignKey(Material, on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    operation_type = models.CharField(max_length=100, choices=OperationType.choices)
    quantity = models.DecimalField(max_digits=10, decimal_places=2)
    operation_date = models.DateTimeField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class Regulation(models.Model):
    regulation_name = models.CharField(max_length=100, choices=RegulationType.choices)
    description = models.TextField()
    authority = models.ForeignKey('Authority', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

class ComplianceCheck(models.Model):
    regulation = models.ForeignKey(Regulation, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    operation = models.ForeignKey(Operation, on_delete=models.CASCADE)
    status = models.CharField(max_length=100, choices=ComplianceStatus.choices)
    check_date = models.DateTimeField()
    remarks = models.TextField()

class Report(models.Model):
    report_name = models.CharField(max_length=100, choices=ReportType.choices)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    report_data = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

class AuditLog(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    action = models.CharField(max_length=255)
    action_details = models.TextField()
    action_time = models.DateTimeField(auto_now_add=True)

class Authority(models.Model):
    authority_name = models.CharField(max_length=100, choices=AuthorityType.choices)
    contact_info = models.TextField()

class SustainabilityTracking(models.Model):
    operation = models.ForeignKey(Operation, on_delete=models.CASCADE)
    carbon_footprint = models.DecimalField(max_digits=10, decimal_places=2)
    sustainability_measures = models.TextField()

class SupportTicket(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    issue = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    resolution = models.TextField(null=True, blank=True)
