# /opt/docker/dev/service_finder/backend/app/models/identity/identity.py
from __future__ import annotations
import uuid
import enum
from datetime import datetime
from typing import Any, List, Optional, TYPE_CHECKING
from sqlalchemy import String, Boolean, DateTime, ForeignKey, JSON, Numeric, text, Integer, BigInteger, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.dialects.postgresql import UUID as PG_UUID, ENUM as PG_ENUM
from sqlalchemy.sql import func

# MB 2.0: Központi aszinkron adatbázis motorból húzzuk be a Base-t
from app.database import Base

if TYPE_CHECKING:
    from .organization import Organization, OrganizationMember
    from .asset import VehicleOwnership
    from .gamification import UserStats
    from .payment import PaymentIntent, WithdrawalRequest
    from .social import ServiceReview, SocialAccount
    from ..marketplace.service_request import ServiceRequest

class UserRole(str, enum.Enum):
    superadmin = "superadmin"
    admin = "admin"
    region_admin = "region_admin"
    country_admin = "country_admin"
    moderator = "moderator"
    sales_agent = "sales_agent"
    user = "user"
    service_owner = "service_owner"
    fleet_manager = "fleet_manager"
    driver = "driver"

class Person(Base):
    """
    Természetes személy identitása. A DNS szint.
    Minden identitás adat az 'identity' sémába kerül.
    """
    __tablename__ = "persons"
    __table_args__ = {"schema": "identity"}
    
    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, index=True)
    id_uuid: Mapped[uuid.UUID] = mapped_column(PG_UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False)
    
    # A lakcím a 'system' sémában van
    address_id: Mapped[Optional[uuid.UUID]] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("system.addresses.id"))
    
    # Kritikus azonosító: Név + Anyja neve + Szül.idő hash-elve.
    identity_hash: Mapped[Optional[str]] = mapped_column(String(64), unique=True, index=True)
    
    last_name: Mapped[str] = mapped_column(String, nullable=False)
    first_name: Mapped[str] = mapped_column(String, nullable=False)
    phone: Mapped[Optional[str]] = mapped_column(String)
    
    mothers_last_name: Mapped[Optional[str]] = mapped_column(String)
    mothers_first_name: Mapped[Optional[str]] = mapped_column(String)
    birth_place: Mapped[Optional[str]] = mapped_column(String)
    birth_date: Mapped[Optional[datetime]] = mapped_column(DateTime)
    
    identity_docs: Mapped[Any] = mapped_column(JSON, nullable=False, default=lambda: {}, server_default=text("'{}'::jsonb"))
    ice_contact: Mapped[Any] = mapped_column(JSON, nullable=False, default=lambda: {}, server_default=text("'{}'::jsonb"))
    
    lifetime_xp: Mapped[int] = mapped_column(BigInteger, default=-1, nullable=False)
    penalty_points: Mapped[int] = mapped_column(Integer, default=-1, nullable=False)
    social_reputation: Mapped[float] = mapped_column(Numeric(3, 2), default=0.0, nullable=False)
    
    is_sales_agent: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
    is_ghost: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=func.now(), nullable=False)
    updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), onupdate=func.now())
    
    # --- KAPCSOLATOK ---
    
    # JAVÍTÁS 1: Explicit 'foreign_keys' megadás az AmbiguousForeignKeysError ellen
    users: Mapped[List["User"]] = relationship(
        "User", 
        foreign_keys="[User.person_id]", 
        back_populates="person",
        cascade="all, delete-orphan"
    )

    # JAVÍTÁS 2: 'post_update' és 'use_alter' a körbe-függőség (circular cycle) feloldásához
    active_user_account: Mapped[Optional["User"]] = relationship(
        "User",
        foreign_keys="[Person.user_id]",
        post_update=True 
    )
    user_id: Mapped[Optional[int]] = mapped_column(
        Integer, 
        ForeignKey("identity.users.id", use_alter=True, name="fk_person_active_user"), 
        nullable=True
    )

    memberships: Mapped[List["OrganizationMember"]] = relationship("OrganizationMember", back_populates="person")
    
    # Kapcsolat a tulajdonolt szervezetekhez (Organization táblában legal_owner_id)
    owned_business_entities: Mapped[List["Organization"]] = relationship(
        "Organization",
        foreign_keys="[Organization.legal_owner_id]",
        back_populates="legal_owner"
    )
    
class User(Base):
    """ Login entitás. Bármikor törölhető (GDPR), de Person-höz kötött. """
    __tablename__ = "users"
    __table_args__ = {"schema": "identity"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    email: Mapped[str] = mapped_column(String, unique=True, index=True, nullable=False)
    hashed_password: Mapped[Optional[str]] = mapped_column(String)
    
    role: Mapped[UserRole] = mapped_column(
        PG_ENUM(UserRole, name="userrole", schema="identity"), 
        default=UserRole.user
    )
    
    person_id: Mapped[Optional[int]] = mapped_column(BigInteger, ForeignKey("identity.persons.id"))
    
    subscription_plan: Mapped[str] = mapped_column(String(30), server_default=text("'FREE'"))
    subscription_expires_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
    is_vip: Mapped[bool] = mapped_column(Boolean, server_default=text("false"))
    
    referral_code: Mapped[Optional[str]] = mapped_column(String(20), unique=True)
    
    # JAVÍTÁS 3: Az ajánló és értékesítő mezőknek is kell a tiszta kapcsolat nevesítés
    referred_by_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
    current_sales_agent_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))

    is_active: Mapped[bool] = mapped_column(Boolean, default=False)
    is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
    folder_slug: Mapped[Optional[str]] = mapped_column(String(12), unique=True, index=True)
    
    preferred_language: Mapped[str] = mapped_column(String(5), server_default="hu")
    region_code: Mapped[str] = mapped_column(String(5), server_default="HU")
    preferred_currency: Mapped[str] = mapped_column(String(3), server_default="HUF")
    ui_mode: Mapped[str] = mapped_column(String(20), server_default="personal")
    
    scope_level: Mapped[str] = mapped_column(String(30), server_default="individual")
    scope_id: Mapped[Optional[str]] = mapped_column(String(50))
    custom_permissions: Mapped[Any] = mapped_column(JSON, server_default=text("'{}'::jsonb"))
    
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    
    # --- KAPCSOLATOK ---

    # JAVÍTÁS 4: Itt is explicit megadjuk, hogy melyik kulcs köti az emberhez
    person: Mapped[Optional["Person"]] = relationship(
        "Person", 
        foreign_keys=[person_id], 
        back_populates="users"
    )

    # JAVÍTÁS 5: Ajánlói (Referrer) önhivatkozó kapcsolat feloldása
    referrer: Mapped[Optional["User"]] = relationship(
        "User",
        remote_side=[id],
        foreign_keys=[referred_by_id]
    )

    # JAVÍTÁS 6: Értékesítő (Sales Agent) önhivatkozó kapcsolat feloldása
    sales_agent: Mapped[Optional["User"]] = relationship(
        "User",
        remote_side=[id],
        foreign_keys=[current_sales_agent_id]
    )

    wallet: Mapped[Optional["Wallet"]] = relationship("Wallet", back_populates="user", uselist=False)
    payment_intents_as_payer = relationship("PaymentIntent", foreign_keys="[PaymentIntent.payer_id]", back_populates="payer")
    payment_intents_as_beneficiary = relationship("PaymentIntent", foreign_keys="[PaymentIntent.beneficiary_id]", back_populates="beneficiary")
    
    trust_profile: Mapped[Optional["UserTrustProfile"]] = relationship("UserTrustProfile", back_populates="user", uselist=False, cascade="all, delete-orphan")
    
    social_accounts: Mapped[List["SocialAccount"]] = relationship("SocialAccount", back_populates="user", cascade="all, delete-orphan")
    owned_organizations: Mapped[List["Organization"]] = relationship("Organization", back_populates="owner")
    stats: Mapped[Optional["UserStats"]] = relationship("UserStats", back_populates="user", uselist=False, cascade="all, delete-orphan")
    ownership_history: Mapped[List["VehicleOwnership"]] = relationship("VehicleOwnership", back_populates="user")
    
    # MB 2.1: Vehicle ratings kapcsolat (hiányzott a listából, visszatéve)
    vehicle_ratings: Mapped[List["VehicleUserRating"]] = relationship("VehicleUserRating", back_populates="user", cascade="all, delete-orphan")

    # Pénzügyi és egyéb kapcsolatok
    withdrawal_requests: Mapped[List["WithdrawalRequest"]] = relationship("WithdrawalRequest", foreign_keys="[WithdrawalRequest.user_id]", back_populates="user", cascade="all, delete-orphan")
    service_reviews: Mapped[List["ServiceReview"]] = relationship("ServiceReview", back_populates="user", cascade="all, delete-orphan")
    service_requests: Mapped[List["ServiceRequest"]] = relationship("ServiceRequest", back_populates="user", cascade="all, delete-orphan")

class Wallet(Base):
    """ Felhasználói pénztárca. """
    __tablename__ = "wallets"
    __table_args__ = {"schema": "identity"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id"), unique=True)
    
    earned_credits: Mapped[float] = mapped_column(Numeric(18, 4), server_default=text("0"))
    purchased_credits: Mapped[float] = mapped_column(Numeric(18, 4), server_default=text("0"))
    service_coins: Mapped[float] = mapped_column(Numeric(18, 4), server_default=text("0"))
    
    currency: Mapped[str] = mapped_column(String(3), default="HUF")
    user: Mapped["User"] = relationship("User", back_populates="wallet")
    active_vouchers: Mapped[List["ActiveVoucher"]] = relationship("ActiveVoucher", back_populates="wallet", cascade="all, delete-orphan")

class VerificationToken(Base):
    """ E-mail és egyéb verifikációs tokenek. """
    __tablename__ = "verification_tokens"
    __table_args__ = {"schema": "identity"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    token: Mapped[uuid.UUID] = mapped_column(PG_UUID(as_uuid=True), default=uuid.uuid4, unique=True, nullable=False)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id", ondelete="CASCADE"), nullable=False)
    token_type: Mapped[str] = mapped_column(String(20), nullable=False)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    is_used: Mapped[bool] = mapped_column(Boolean, default=False)

class SocialAccount(Base):
    """ Közösségi bejelentkezési adatok (Google, Facebook, stb). """
    __tablename__ = "social_accounts"
    __table_args__ = (
        UniqueConstraint('provider', 'social_id', name='uix_social_provider_id'), 
        {"schema": "identity"}
    )
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id", ondelete="CASCADE"), nullable=False)
    provider: Mapped[str] = mapped_column(String(50), nullable=False)
    social_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
    email: Mapped[str] = mapped_column(String(255), nullable=False)
    extra_data: Mapped[Any] = mapped_column(JSON, server_default=text("'{}'::jsonb"))
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    
    user: Mapped["User"] = relationship("User", back_populates="social_accounts")

class ActiveVoucher(Base):
    """ Aktív, le nem járt voucher-ek tárolása FIFO elv szerint. """
    __tablename__ = "active_vouchers"
    __table_args__ = {"schema": "identity"}
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
    wallet_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.wallets.id", ondelete="CASCADE"), nullable=False)
    amount: Mapped[float] = mapped_column(Numeric(18, 4), nullable=False)
    original_amount: Mapped[float] = mapped_column(Numeric(18, 4), nullable=False)
    expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    
    wallet: Mapped["Wallet"] = relationship("Wallet", back_populates="active_vouchers")

class UserTrustProfile(Base):
    """ Gondos Gazda Index (Trust Score) tárolása. """
    __tablename__ = "user_trust_profiles"
    __table_args__ = {"schema": "identity"}
    
    user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("identity.users.id", ondelete="CASCADE"),
        primary_key=True,
        index=True
    )
    trust_score: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    maintenance_score: Mapped[float] = mapped_column(Numeric(5, 2), default=0.0, nullable=False)
    quality_score: Mapped[float] = mapped_column(Numeric(5, 2), default=0.0, nullable=False)
    preventive_score: Mapped[float] = mapped_column(Numeric(5, 2), default=0.0, nullable=False)
    last_calculated: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
        nullable=False
    )
    
    user: Mapped["User"] = relationship("User", back_populates="trust_profile", uselist=False)