from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Text, JSON, Float
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..database import Base


class VehicleRequest(Base):
    """Track vehicle search requests from users"""
    __tablename__ = "vehicle_requests"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

    # Search criteria
    maker_code = Column(String(50))
    maker_name = Column(String(100))
    model_code = Column(String(50))
    model_name = Column(String(100))
    grade_code = Column(String(50))
    grade_name = Column(String(100))
    year_from = Column(Integer)
    year_to = Column(Integer)
    mileage_min = Column(Integer)
    mileage_max = Column(Integer)
    fuel = Column(String(50))  # 연료 타입 (휘발유, 경유, 하이브리드, LPG, 전기)
    displacement_min = Column(Integer)  # 최소 배기량 (cc)
    displacement_max = Column(Integer)  # 최대 배기량 (cc)

    # CC payment for request submission
    cc_paid = Column(Float, default=1.0)  # CC paid for this request (1 CC)

    # Status: pending, reviewed, completed
    status = Column(String(20), default="pending")
    admin_reviewed_at = Column(DateTime(timezone=True))
    admin_notes = Column(Text)

    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    # Relationships
    user = relationship("User", backref="vehicle_requests")
    recommended_vehicles = relationship("RequestVehicle", back_populates="request", cascade="all, delete-orphan")


class RequestVehicle(Base):
    """Vehicles recommended by admin for a user's request"""
    __tablename__ = "request_vehicles"

    id = Column(Integer, primary_key=True, index=True)
    request_id = Column(Integer, ForeignKey("vehicle_requests.id"), nullable=False)

    # Car data from Carmodoo (stored as JSON)
    car_data = Column(JSON, nullable=False)

    # Admin approval
    is_approved = Column(Boolean, default=False)
    approved_at = Column(DateTime(timezone=True))

    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    request = relationship("VehicleRequest", back_populates="recommended_vehicles")


class PurchasedVehicle(Base):
    """Track purchased vehicles and their shipping status"""
    __tablename__ = "purchased_vehicles"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)

    # Vehicle info
    car_name = Column(String(200))
    car_data = Column(JSON)  # Full car details
    car_image = Column(String(500))  # Main image URL

    # Price info
    vehicle_price_krw = Column(Integer)
    domestic_cost_krw = Column(Integer)
    shipping_cost_usd = Column(Integer)
    total_cost_krw = Column(Integer)
    car_type = Column(String(20))  # small, compact

    # Dealer selection and commission (50/50 split of Mongolia margin)
    selected_dealer_id = Column(Integer, ForeignKey("dealer_info.id"), nullable=True)
    dealer_commission_krw = Column(Integer, default=0)  # 50% of Mongolia margin
    platform_commission_krw = Column(Integer, default=0)  # 50% of Mongolia margin
    commission_paid = Column(Boolean, default=False)  # Whether commission has been paid
    commission_paid_at = Column(DateTime(timezone=True))

    # Shipping status: 1-5
    # 1: Purchased, 2: Incheon Port, 3: In Transit, 4: Customs, 5: Delivered
    shipping_status = Column(Integer, default=1)
    status_updated_at = Column(DateTime(timezone=True))

    # Location info
    current_location = Column(String(200))
    estimated_arrival = Column(DateTime(timezone=True))

    # Timestamps
    purchased_at = Column(DateTime(timezone=True), server_default=func.now())
    delivered_at = Column(DateTime(timezone=True))

    # Relationships
    user = relationship("User", backref="purchased_vehicles")
    selected_dealer = relationship("DealerInfo", backref="purchased_vehicles")
