"""
Restaurant, Market, and Adventure models
"""
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, DateTime, Text, ForeignKey, DECIMAL, Time, Date
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
import uuid
from app.db.session import Base


class Restaurant(Base):
    __tablename__ = "restaurants"

    restaurant_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    restaurant_name = Column(String(100), nullable=False)
    company_id = Column(PG_UUID(as_uuid=True), ForeignKey("companies.company_id"))
    beach_place_id = Column(PG_UUID(as_uuid=True), ForeignKey("beach_places.beach_place_id"))
    description = Column(Text)
    cuisine_type = Column(String(50))
    address = Column(String(500))
    city = Column(String(100))
    country_id = Column(Integer, ForeignKey("countries.country_id"))
    latitude = Column(DECIMAL(10, 8))
    longitude = Column(DECIMAL(11, 8))
    opening_time = Column(Time)
    closing_time = Column(Time)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    company = relationship("Company", back_populates="restaurants")
    beach_place = relationship("BeachPlace", back_populates="restaurants")
    country = relationship("Country")
    categories = relationship("RestaurantCategory", back_populates="restaurant", cascade="all, delete-orphan")
    items = relationship("RestaurantItem", back_populates="restaurant", cascade="all, delete-orphan")
    photos = relationship("RestaurantPhoto", back_populates="restaurant", cascade="all, delete-orphan")


class RestaurantPhoto(Base):
    __tablename__ = "restaurant_photos"

    photo_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    restaurant_id = Column(PG_UUID(as_uuid=True), ForeignKey("restaurants.restaurant_id"), nullable=False)
    photo_path = Column(String(1000), nullable=False)
    photo_url = Column(String(2000))
    photo_caption = Column(Text)
    is_primary = Column(Boolean, default=False)
    photo_type = Column(String(50), default='general')  # 'general', 'interior', 'exterior', 'food', 'logo'
    file_size = Column(BigInteger)
    width = Column(Integer)
    height = Column(Integer)
    uploaded_at = Column(DateTime(timezone=True), server_default=func.now())
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    restaurant = relationship("Restaurant", back_populates="photos")


class RestaurantCategory(Base):
    __tablename__ = "restaurant_categories"

    category_id = Column(BigInteger, primary_key=True, index=True)
    restaurant_id = Column(PG_UUID(as_uuid=True), ForeignKey("restaurants.restaurant_id"))
    category_name = Column(String(100), nullable=False)
    description = Column(Text)
    sort_order = Column(Integer, default=0)

    # Relationships
    restaurant = relationship("Restaurant", back_populates="categories")
    items = relationship("RestaurantItem", back_populates="category")


class RestaurantItem(Base):
    __tablename__ = "restaurant_items"

    item_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    restaurant_id = Column(PG_UUID(as_uuid=True), ForeignKey("restaurants.restaurant_id"))
    category_id = Column(BigInteger, ForeignKey("restaurant_categories.category_id"))
    item_name = Column(String(100), nullable=False)
    description = Column(Text)
    price = Column(DECIMAL(10, 2), nullable=False)
    currency_id = Column(BigInteger, ForeignKey("currencies.currency_id"))
    photo_path = Column(String(1000))
    is_available = Column(Boolean, default=True)
    preparation_time = Column(Integer)  # in minutes
    allergens = Column(Text)
    is_vegetarian = Column(Boolean, default=False)
    is_vegan = Column(Boolean, default=False)
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    restaurant = relationship("Restaurant", back_populates="items")
    category = relationship("RestaurantCategory", back_populates="items")
    currency = relationship("Currency")


class MarketPhoto(Base):
    __tablename__ = "market_photos"

    photo_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    market_id = Column(PG_UUID(as_uuid=True), ForeignKey("markets.market_id"), nullable=False)
    photo_path = Column(String(1000), nullable=False)
    photo_url = Column(String(2000))
    photo_caption = Column(Text)
    is_primary = Column(Boolean, default=False)
    photo_type = Column(String(50), default='general')  # 'general', 'interior', 'exterior', 'products', 'logo'
    file_size = Column(BigInteger)
    width = Column(Integer)
    height = Column(Integer)
    uploaded_at = Column(DateTime(timezone=True), server_default=func.now())
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    market = relationship("Market", back_populates="photos")


class Market(Base):
    __tablename__ = "markets"

    market_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    market_name = Column(String(100), nullable=False)
    company_id = Column(PG_UUID(as_uuid=True), ForeignKey("companies.company_id"))
    beach_place_id = Column(PG_UUID(as_uuid=True), ForeignKey("beach_places.beach_place_id"))
    description = Column(Text)
    address = Column(String(255))
    city = Column(String(100))
    country_id = Column(BigInteger, ForeignKey("countries.country_id"))
    latitude = Column(DECIMAL(10, 8))
    longitude = Column(DECIMAL(11, 8))
    phone = Column(String(50))
    email = Column(String(255))
    website = Column(String(500))
    opening_time = Column(Time)
    closing_time = Column(Time)
    delivery_available = Column(Boolean, default=True)
    delivery_fee = Column(DECIMAL(10, 2), default=0)
    min_order_amount = Column(DECIMAL(10, 2), default=0)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Relationships
    company = relationship("Company", back_populates="markets")
    beach_place = relationship("BeachPlace", back_populates="markets")
    country = relationship("Country")
    categories = relationship("MarketCategory", back_populates="market", cascade="all, delete-orphan")
    items = relationship("MarketItem", back_populates="market", cascade="all, delete-orphan")
    photos = relationship("MarketPhoto", back_populates="market", cascade="all, delete-orphan")


class MarketCategory(Base):
    __tablename__ = "market_categories"

    category_id = Column(BigInteger, primary_key=True, index=True)
    market_id = Column(PG_UUID(as_uuid=True), ForeignKey("markets.market_id"))
    category_name = Column(String(100), nullable=False)
    description = Column(Text)
    sort_order = Column(Integer, default=0)

    # Relationships
    market = relationship("Market", back_populates="categories")
    items = relationship("MarketItem", back_populates="category")


class MarketItem(Base):
    __tablename__ = "market_items"

    item_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    market_id = Column(PG_UUID(as_uuid=True), ForeignKey("markets.market_id"))
    category_id = Column(BigInteger, ForeignKey("market_categories.category_id"))
    item_name = Column(String(100), nullable=False)
    description = Column(Text)
    price = Column(DECIMAL(10, 2), nullable=False)
    currency_id = Column(BigInteger, ForeignKey("currencies.currency_id"))
    photo_path = Column(String(1000))
    stock_quantity = Column(Integer, default=0)
    is_available = Column(Boolean, default=True)
    brand = Column(String(50))
    barcode = Column(String(50))
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    market = relationship("Market", back_populates="items")
    category = relationship("MarketCategory", back_populates="items")
    currency = relationship("Currency")


class Adventure(Base):
    __tablename__ = "adventures"

    adventure_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    adventure_name = Column(String(100), nullable=False)
    company_id = Column(PG_UUID(as_uuid=True), ForeignKey("companies.company_id"))
    beach_place_id = Column(PG_UUID(as_uuid=True), ForeignKey("beach_places.beach_place_id"))
    description = Column(Text)
    address = Column(String(255))
    city = Column(String(100))
    country_id = Column(BigInteger, ForeignKey("countries.country_id"))
    latitude = Column(DECIMAL(10, 8))
    longitude = Column(DECIMAL(11, 8))
    phone = Column(String(50))
    email = Column(String(255))
    website = Column(String(500))
    duration_minutes = Column(Integer)
    max_participants = Column(Integer)
    min_participants = Column(Integer, default=1)
    price = Column(DECIMAL(10, 2), nullable=False)
    currency_id = Column(BigInteger, ForeignKey("currencies.currency_id"))
    equipment_provided = Column(Text)
    requirements = Column(Text)
    safety_instructions = Column(Text)
    photo_path = Column(String(1000))
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Relationships
    company = relationship("Company", back_populates="adventures")
    beach_place = relationship("BeachPlace", back_populates="adventures")
    country = relationship("Country")
    currency = relationship("Currency")
    schedules = relationship("AdventureSchedule", back_populates="adventure", cascade="all, delete-orphan")
    photos = relationship("AdventurePhoto", back_populates="adventure", cascade="all, delete-orphan")


class AdventurePhoto(Base):
    __tablename__ = "adventure_photos"

    photo_id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    adventure_id = Column(PG_UUID(as_uuid=True), ForeignKey("adventures.adventure_id"), nullable=False)
    photo_path = Column(String(1000), nullable=False)
    photo_url = Column(String(2000))
    photo_caption = Column(Text)
    is_primary = Column(Boolean, default=False)
    photo_type = Column(String(50), default='general')  # 'general', 'equipment', 'location', 'action', 'logo'
    file_size = Column(BigInteger)
    width = Column(Integer)
    height = Column(Integer)
    uploaded_at = Column(DateTime(timezone=True), server_default=func.now())
    created_at = Column(DateTime(timezone=True), server_default=func.now())

    # Relationships
    adventure = relationship("Adventure", back_populates="photos")


class AdventureSchedule(Base):
    __tablename__ = "adventure_schedules"

    schedule_id = Column(BigInteger, primary_key=True, index=True)
    adventure_id = Column(PG_UUID(as_uuid=True), ForeignKey("adventures.adventure_id"))
    date = Column(Date, nullable=False)
    start_time = Column(Time, nullable=False)
    available_spots = Column(Integer, nullable=False)
    is_cancelled = Column(Boolean, default=False)

    # Relationships
    adventure = relationship("Adventure", back_populates="schedules")