# /opt/docker/dev/service_finder/backend/app/models/identity/address.py
import uuid
from datetime import datetime
from typing import Any, List, Optional
from sqlalchemy import String, Integer, ForeignKey, Text, DateTime, Float, Boolean, text, func, Numeric, Index, and_
from sqlalchemy.dialects.postgresql import UUID as PG_UUID, JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship, foreign

# MB 2.0: Kritikus javítás - a központi metadata-t használjuk az app.database-ből
from app.database import Base

class GeoPostalCode(Base):
    """Irányítószám alapú földrajzi kereső tábla."""
    __tablename__ = "geo_postal_codes"
    __table_args__ = {"schema": "system"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    country_code: Mapped[str] = mapped_column(String(5), default="HU")
    zip_code: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
    city: Mapped[str] = mapped_column(String(100), nullable=False, index=True)

class GeoStreet(Base):
    """Utcajegyzék tábla."""
    __tablename__ = "geo_streets"
    __table_args__ = {"schema": "system"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    postal_code_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("system.geo_postal_codes.id"))
    name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)

class GeoStreetType(Base):
    """Közterület jellege (utca, út, köz stb.)."""
    __tablename__ = "geo_street_types"
    __table_args__ = {"schema": "system"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(50), unique=True, nullable=False)

class Address(Base):
    """Univerzális cím entitás GPS adatokkal kiegészítve."""
    __tablename__ = "addresses"
    __table_args__ = {"schema": "system"}

    id: Mapped[uuid.UUID] = mapped_column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    postal_code_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("system.geo_postal_codes.id"))
    
    street_name: Mapped[str] = mapped_column(String(200), nullable=False)
    street_type: Mapped[str] = mapped_column(String(50), nullable=False)
    house_number: Mapped[str] = mapped_column(String(50), nullable=False)
    
    stairwell: Mapped[Optional[str]] = mapped_column(String(20))
    floor: Mapped[Optional[str]] = mapped_column(String(20))
    door: Mapped[Optional[str]] = mapped_column(String(20))
    parcel_id: Mapped[Optional[str]] = mapped_column(String(50))
    full_address_text: Mapped[Optional[str]] = mapped_column(Text)
    
    # Robot és térképes funkciók számára
    latitude: Mapped[Optional[float]] = mapped_column(Float)
    longitude: Mapped[Optional[float]] = mapped_column(Float)
    
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())



class Rating(Base):
    """Univerzális értékelési rendszer - v1.3.1"""
    __tablename__ = "ratings"
    __table_args__ = (
        Index('idx_rating_org', 'target_organization_id'),
        Index('idx_rating_user', 'target_user_id'),
        Index('idx_rating_branch', 'target_branch_id'),
        {"schema": "marketplace"}
    )

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    
    # MB 2.0: A felhasználók az identity sémában laknak!
    author_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id"), nullable=False)
    
    target_organization_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("fleet.organizations.id"))
    target_user_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    target_branch_id: Mapped[Optional[uuid.UUID]] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("fleet.branches.id"))
    
    score: Mapped[float] = mapped_column(Numeric(3, 2), nullable=False)
    comment: Mapped[Optional[str]] = mapped_column(Text)
    images: Mapped[Any] = mapped_column(JSONB, server_default=text("'[]'::jsonb"))
    
    is_verified: Mapped[bool] = mapped_column(Boolean, default=False)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) 