# /opt/docker/dev/service_finder/backend/app/models/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

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 'data' sémában marad
    address_id: Mapped[Optional[uuid.UUID]] = mapped_column(PG_UUID(as_uuid=True), ForeignKey("data.addresses.id"))
    
    # Kritikus azonosító: Név + Anyja neve + Szül.idő hash-elve.
    # Ezzel ismerjük fel a személyt akkor is, ha új User accountot hoz létre.
    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, server_default=text("'{}'::jsonb"))
    ice_contact: Mapped[Any] = mapped_column(JSON, server_default=text("'{}'::jsonb"))
    
    lifetime_xp: Mapped[int] = mapped_column(BigInteger, server_default=text("0"))
    penalty_points: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    social_reputation: Mapped[float] = mapped_column(Numeric(3, 2), server_default=text("1.00"))
    
    is_sales_agent: Mapped[bool] = mapped_column(Boolean, server_default=text("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), server_default=func.now())
    updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), onupdate=func.now())
    
    # --- KAPCSOLATOK ---
    users: Mapped[List["User"]] = relationship("User", back_populates="person")
    memberships: Mapped[List["OrganizationMember"]] = relationship("OrganizationMember", back_populates="person")
    
    # MB 2.0 KIEGÉSZÍTÉS: A személy által birtokolt üzleti entitások (Cégek/Szolgáltatók)
    # Ez a lista megmarad akkor is, ha az Organization deaktiválódik.
    owned_business_entities: Mapped[List["Organization"]] = relationship("Organization", 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)
    
    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")
    
    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
    person: Mapped[Optional["Person"]] = relationship("Person", back_populates="users")
    wallet: Mapped[Optional["Wallet"]] = relationship("Wallet", back_populates="user", uselist=False)
    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")
    
    # PaymentIntent kapcsolatok
    payment_intents_as_payer: Mapped[List["PaymentIntent"]] = relationship(
        "PaymentIntent",
        foreign_keys="[PaymentIntent.payer_id]",
        back_populates="payer"
    )
    withdrawal_requests: Mapped[List["WithdrawalRequest"]] = relationship("WithdrawalRequest", foreign_keys="[WithdrawalRequest.user_id]", back_populates="user", cascade="all, delete-orphan")
    payment_intents_as_beneficiary: Mapped[List["PaymentIntent"]] = relationship(
        "PaymentIntent",
        foreign_keys="[PaymentIntent.beneficiary_id]",
        back_populates="beneficiary"
    )

    @property
    def tier_name(self) -> str:
        """Kompatibilitási mező a keresőhöz: a 'FREE' -> 'free' konverzióhoz"""
        return (self.subscription_plan or "free").lower()

class Wallet(Base):
    __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):
    __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):
    __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())
    
    # Kapcsolatok
    wallet: Mapped["Wallet"] = relationship("Wallet", back_populates="active_vouchers")