"""
Offer Profile Model
Driver automatic offer configuration for ride requests
"""
import enum
from typing import Optional, List
from sqlalchemy import (
    String, Boolean, Integer, Float, ForeignKey, JSON,
    Enum, Index
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.models.base import BaseModel


class PriceType(str, enum.Enum):
    """Offer pricing type"""
    PRICE_PER_KM = "price_per_km"
    FIXED_PRICE = "fixed_price"


class OfferProfile(BaseModel):
    """Offer profile for automatic ride offers"""
    __tablename__ = "offer_profiles"

    driver_id: Mapped[int] = mapped_column(
        ForeignKey("drivers.id", ondelete="CASCADE"),
        nullable=False,
        index=True
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False)
    enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    confirm_offer_manually: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    priority: Mapped[int] = mapped_column(Integer, default=0, nullable=False)

    start_area: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
    destination_area: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
    distance_min_km: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    distance_max_km: Mapped[Optional[float]] = mapped_column(Float, nullable=True)

    price_type: Mapped[PriceType] = mapped_column(
        Enum(PriceType),
        default=PriceType.PRICE_PER_KM,
        nullable=False
    )
    price_per_km: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    fixed_price: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    initial_charge: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    round_trip_enabled: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    round_trip_discount_pct: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    child_seat_charge: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)

    start_time: Mapped[Optional[str]] = mapped_column(String(5), nullable=True)
    end_time: Mapped[Optional[str]] = mapped_column(String(5), nullable=True)
    pre_ride_time_min: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    min_lead_time_min: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    days_of_week: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)

    for_all_vehicles: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    vehicle_ids: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)

    driver: Mapped["Driver"] = relationship(
        "Driver",
        back_populates="offer_profiles",
        foreign_keys=[driver_id]
    )

    __table_args__ = (
        Index('idx_offer_profile_driver_enabled', 'driver_id', 'enabled'),
    )
