from __future__ import annotations
import uuid
from datetime import datetime, date
from sqlalchemy import Column, String, Float, Integer, Text, Boolean, Date, DateTime, ForeignKey, func
from sqlalchemy.orm import relationship
from app.database import Base


class PlantPassport(Base):
    __tablename__ = "plant_passports"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id = Column(String(30), unique=True, nullable=False)  # CHR-PLT-2026-000001
    batch_id = Column(String(36), ForeignKey("production_batches.id"), nullable=False)
    plant_id = Column(String(36), ForeignKey("plants.id"), nullable=False)
    variety_id = Column(String(36), ForeignKey("plant_varieties.id"), nullable=True)
    category_id = Column(String(36), ForeignKey("plant_categories.id"), nullable=True)
    branch_id = Column(String(36), ForeignKey("branches.id"), nullable=True)

    plant_name = Column(String(100), nullable=True)
    scientific_name = Column(String(100), nullable=True)
    local_name = Column(String(100), nullable=True)
    variety_name = Column(String(100), nullable=True)
    category_name = Column(String(100), nullable=True)

    batch_number = Column(String(30), nullable=True)
    seed_lot_number = Column(String(50), nullable=True)
    seed_supplier = Column(String(100), nullable=True)
    sowing_date = Column(Date, nullable=True)
    germination_date = Column(Date, nullable=True)
    germination_rate = Column(Float, nullable=True)
    transplant_date = Column(Date, nullable=True)
    current_growth_stage = Column(String(50), default="sowing")
    expected_ready_date = Column(Date, nullable=True)
    ready_for_sale_date = Column(Date, nullable=True)

    current_stock = Column(Integer, default=0)
    nursery_zone = Column(String(50), nullable=True)
    greenhouse = Column(String(50), nullable=True)
    bed_number = Column(String(50), nullable=True)
    tray_number = Column(String(50), nullable=True)
    pot_number = Column(String(50), nullable=True)
    warehouse_location = Column(String(100), nullable=True)
    location = Column(String(100), nullable=True)

    production_cost = Column(Float, default=0.0)
    selling_price = Column(Float, default=0.0)
    wholesale_price = Column(Float, default=0.0)
    discount_price = Column(Float, default=0.0)
    profit_margin = Column(Float, default=0.0)

    health_status = Column(String(20), default="healthy")
    health_score = Column(Float, nullable=True)
    leaf_color = Column(String(50), nullable=True)
    plant_height_cm = Column(Float, nullable=True)
    root_condition = Column(String(50), nullable=True)
    stem_thickness_mm = Column(Float, nullable=True)
    disease_status = Column(String(50), nullable=True)
    pest_status = Column(String(50), nullable=True)
    mortality_risk = Column(String(20), default="low")

    qr_code = Column(Text, nullable=True)  # base64 QR image
    barcode = Column(String(50), nullable=True)
    scan_url = Column(String(500), nullable=True)

    owner_id = Column(String(36), ForeignKey("customers.id"), nullable=True)
    activation_status = Column(String(20), default="inactive")  # inactive, activated, deactivated

    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())

    batch = relationship("ProductionBatch", backref="passports")
    plant = relationship("Plant")
    variety = relationship("PlantVariety")
    category = relationship("PlantCategory")
    branch = relationship("Branch")


class PassportTimelineEvent(Base):
    __tablename__ = "passport_timeline_events"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id = Column(String(36), ForeignKey("plant_passports.id"), nullable=False)
    event_date = Column(Date, nullable=False)
    event_type = Column(String(50), nullable=False)  # sowing, germination, transplant, treatment, health_check, disease, sale
    event_title = Column(String(200), nullable=False)
    event_details = Column(Text, nullable=True)
    icon = Column(String(30), nullable=True)
    photo_url = Column(String(500), nullable=True)
    recorded_by = Column(String(36), ForeignKey("users.id"), nullable=True)
    created_at = Column(DateTime, server_default=func.now())

    passport = relationship("PlantPassport", backref="timeline_events")


class PassportPhoto(Base):
    __tablename__ = "passport_photos"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id = Column(String(36), ForeignKey("plant_passports.id"), nullable=False)
    growth_stage = Column(String(50), nullable=False)
    photo_url = Column(String(500), nullable=False)
    caption = Column(String(200), nullable=True)
    taken_date = Column(Date, nullable=True)
    taken_by = Column(String(36), ForeignKey("users.id"), nullable=True)
    created_at = Column(DateTime, server_default=func.now())

    passport = relationship("PlantPassport", backref="photos")


class PassportScanLog(Base):
    __tablename__ = "passport_scan_logs"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id = Column(String(36), ForeignKey("plant_passports.id"), nullable=False)
    scanned_by = Column(String(100), nullable=True)
    scan_source = Column(String(30), default="qr_code")  # qr_code, barcode, manual
    device_info = Column(String(200), nullable=True)
    ip_address = Column(String(50), nullable=True)
    scanned_at = Column(DateTime, server_default=func.now())

    passport = relationship("PlantPassport", backref="scan_logs")


class PassportCustomerView(Base):
    __tablename__ = "passport_customer_views"

    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    passport_id = Column(String(36), ForeignKey("plant_passports.id"), nullable=False)
    customer_id = Column(String(36), ForeignKey("customers.id"), nullable=True)
    sale_id = Column(String(36), ForeignKey("sales.id"), nullable=True)
    sale_date = Column(Date, nullable=True)
    invoice_number = Column(String(30), nullable=True)
    delivery_status = Column(String(30), default="pending")
    warranty_expiry = Column(Date, nullable=True)
    created_at = Column(DateTime, server_default=func.now())

    passport = relationship("PlantPassport", backref="customer_views")
    customer = relationship("Customer")
    sale = relationship("Sale")
