"""
Client Model - Business accounts
"""
from sqlalchemy import Column, String, Boolean, Text, JSON
from sqlalchemy.orm import relationship
from enum import Enum

from app.models.base import BaseModel


class ClientStatus(str, Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    SUSPENDED = "suspended"
    TRIAL = "trial"
    CANCELLED = "cancelled"


class Client(BaseModel):
    """
    Client/Company model
    Each client can have multiple users and subscriptions
    """
    __tablename__ = "clients"

    # Company Info
    company_name = Column(String(255), nullable=False, index=True)
    company_email = Column(String(255), unique=True, nullable=False, index=True)
    company_phone = Column(String(50), nullable=True)
    company_website = Column(String(255), nullable=True)
    company_logo_url = Column(String(500), nullable=True)

    # Address
    address_line1 = Column(String(255), nullable=True)
    address_line2 = Column(String(255), nullable=True)
    city = Column(String(100), nullable=True)
    state = Column(String(100), nullable=True)
    country = Column(String(100), default="India", nullable=True)
    postal_code = Column(String(20), nullable=True)

    # Business Info
    industry = Column(String(100), nullable=True)
    company_size = Column(String(50), nullable=True)  # 1-10, 11-50, 51-200, 201-500, 500+
    gstin = Column(String(20), nullable=True)  # India GST Number
    pan = Column(String(20), nullable=True)    # PAN Number

    # Status
    status = Column(String(50), default=ClientStatus.TRIAL, nullable=False)
    is_verified = Column(Boolean, default=False)

    # Billing
    billing_email = Column(String(255), nullable=True)
    billing_address = Column(Text, nullable=True)
    stripe_customer_id = Column(String(100), nullable=True)
    razorpay_customer_id = Column(String(100), nullable=True)
    preferred_payment_method = Column(String(50), default="razorpay")  # stripe, razorpay

    # Settings
    settings = Column(JSON, default={})
    extra_data = Column("metadata", JSON, default={})  # Renamed to avoid SQLAlchemy reserved word

    # Notes (internal)
    internal_notes = Column(Text, nullable=True)

    # Referral
    referred_by = Column(String(100), nullable=True)
    referral_code = Column(String(50), unique=True, nullable=True)

    # Relationships
    users = relationship("User", back_populates="client", cascade="all, delete-orphan")
    subscriptions = relationship("Subscription", back_populates="client", cascade="all, delete-orphan")
    invoices = relationship("Invoice", back_populates="client", cascade="all, delete-orphan")
    usage_logs = relationship("UsageLog", back_populates="client", cascade="all, delete-orphan")
    service_configs = relationship("ServiceConfig", back_populates="client", cascade="all, delete-orphan")
    service_credentials = relationship("ServiceCredential", back_populates="client", cascade="all, delete-orphan")

    def __repr__(self):
        return f"<Client {self.company_name}>"

    @property
    def is_active(self) -> bool:
        return self.status == ClientStatus.ACTIVE

    @property
    def full_address(self) -> str:
        parts = [
            self.address_line1,
            self.address_line2,
            self.city,
            self.state,
            self.postal_code,
            self.country
        ]
        return ", ".join(filter(None, parts))
