# app/models/dispatch_models.py
"""
Dispatch Module Models
Covers:
  - DispatchThermalChecksheet     : Header record per chassis
  - DispatchThermalChecksheetItem : One row per checklist item (26 items)
  - DispatchGatePass              : Gate pass once thermal is approved
  - DispatchGatePassItem          : Items checked on the gate pass
"""
import uuid
from datetime import datetime, timezone
from sqlalchemy import (
    Column, String, Boolean, DateTime, Date, Text,
    Integer, Numeric, ForeignKey
)
from sqlalchemy.dialects.postgresql import UUID
from app.core.database import Base


def now_utc():
    return datetime.now(timezone.utc)


class DispatchThermalChecksheet(Base):
    """
    One record per chassis dispatch thermal inspection.
    Stores header info; individual item results are in DispatchThermalChecksheetItem.
    """
    __tablename__ = "dispatch_thermal_checksheet"

    checksheet_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    chassis_id    = Column(UUID(as_uuid=True), ForeignKey("chassis_master.chassis_id"), nullable=False)
    chassis_no    = Column(String(100), nullable=False)
    model         = Column(String(100), nullable=True)
    engine_type   = Column(String(50), nullable=True)   # Eng-CNG/Diesel / Electric / CNG / Diesel
    check_date    = Column(Date, nullable=True)
    signature     = Column(String(255), nullable=True)  # Signatory name / designation

    # Bus 4-view photos
    photo_front   = Column(Text, nullable=True)  # /uploads/dispatch/...
    photo_rear    = Column(Text, nullable=True)
    photo_rh      = Column(Text, nullable=True)
    photo_lh      = Column(Text, nullable=True)

    # Overall result
    all_ok        = Column(Boolean, nullable=False, default=False)
    submitted_by  = Column(UUID(as_uuid=True), ForeignKey("login_users.user_id"), nullable=True)
    submitted_at  = Column(DateTime(timezone=True), default=now_utc)
    created_at    = Column(DateTime(timezone=True), default=now_utc)
    updated_at    = Column(DateTime(timezone=True), default=now_utc, onupdate=now_utc)


class DispatchThermalChecksheetItem(Base):
    """
    One row per checklist item (items 1-26 + row 27 signature).
    Stores OK/not-OK, photo, authority, fault type, and mail screenshot.
    """
    __tablename__ = "dispatch_thermal_checksheet_item"

    item_result_id  = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    checksheet_id   = Column(UUID(as_uuid=True), ForeignKey("dispatch_thermal_checksheet.checksheet_id"), nullable=False)
    item_no         = Column(Integer, nullable=False)   # 1-26
    item_text       = Column(Text, nullable=True)       # Hindi label (denormalised for audit)
    is_ok           = Column(Boolean, nullable=False, default=False)

    # Photo taken for this item
    photo_url       = Column(Text, nullable=True)       # /uploads/dispatch/item_photos/...

    # Not-OK resolution fields
    authority_name  = Column(String(255), nullable=True)
    fault_type      = Column(String(30), nullable=True)  # OUR_FAULT | MANUFACTURING
    remarks         = Column(Text, nullable=True)
    mail_screenshot_url = Column(Text, nullable=True)   # /uploads/dispatch/mail_screenshots/...


class DispatchGatePass(Base):
    """
    Gate pass issued once thermal checksheet is submitted.
    Auto-populated with chassis_no and generated_at timestamp.
    """
    __tablename__ = "dispatch_gate_pass"

    gate_pass_id      = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    checksheet_id     = Column(UUID(as_uuid=True), ForeignKey("dispatch_thermal_checksheet.checksheet_id"), nullable=True)
    chassis_id        = Column(UUID(as_uuid=True), ForeignKey("chassis_master.chassis_id"), nullable=False)
    chassis_no        = Column(String(100), nullable=False)
    generated_at      = Column(DateTime(timezone=True), default=now_utc)

    # Fuel
    fuel_before_ltr       = Column(Numeric(10, 2), nullable=True)
    filling_fuel_qty_ltr  = Column(Numeric(10, 2), nullable=True)

    # Driver
    driver_name       = Column(String(255), nullable=True)
    driver_photo_url  = Column(Text, nullable=True)       # /uploads/dispatch/driver_photos/...

    # Dispatch authority
    dispatch_authority = Column(String(255), nullable=True)

    created_by        = Column(UUID(as_uuid=True), ForeignKey("login_users.user_id"), nullable=True)
    created_at        = Column(DateTime(timezone=True), default=now_utc)


class DispatchGatePassItem(Base):
    """
    Items checked on the gate pass (Clam Box, Jack, Urea Key, Stephony, Toolkit, Ignition Key etc).
    """
    __tablename__ = "dispatch_gate_pass_item"

    gate_pass_item_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    gate_pass_id      = Column(UUID(as_uuid=True), ForeignKey("dispatch_gate_pass.gate_pass_id"), nullable=False)
    item_key          = Column(String(50), nullable=False)   # clam_box / jack / urea_key / stephony / toolkit / ignition_key
    item_label        = Column(String(100), nullable=False)
    is_checked        = Column(Boolean, nullable=False, default=True)
    qty               = Column(Integer, nullable=False, default=1)
