from sqlmodel import SQLModel, Field, Column, Relationship
from datetime import datetime
from typing import Optional, List, TYPE_CHECKING
from uuid import UUID, uuid4
from sqlalchemy import DateTime, func

if TYPE_CHECKING:
    from app.models.coupon import Coupon

class Business(SQLModel, table=True):
    """Business record for local partners."""
    
    __tablename__ = "businesses"
    
    id: Optional[UUID] = Field(default_factory=uuid4, primary_key=True)
    name: str
    address: Optional[str] = None
    phone: Optional[str] = None
    image_url: Optional[str] = None
    social_media: Optional[str] = None
    category: Optional[str] = None
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    area: Optional[str] = Field(default="Boquete")
    
    # Relationship to coupons
    coupons: List["Coupon"] = Relationship(back_populates="business")
    
    created_at: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(timezone=True), server_default=func.now())
    )
    updated_at: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
    )

class BusinessCreate(SQLModel):
    name: str
    address: Optional[str] = None
    phone: Optional[str] = None
    image_url: Optional[str] = None
    social_media: Optional[str] = None
    category: Optional[str] = None
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    area: Optional[str] = "Boquete"

class BusinessUpdate(SQLModel):
    name: Optional[str] = None
    address: Optional[str] = None
    phone: Optional[str] = None
    image_url: Optional[str] = None
    social_media: Optional[str] = None
    category: Optional[str] = None
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    area: Optional[str] = None
