from datetime import datetime
from typing import List, Optional

from pydantic import BaseModel, Field

from app.schemas.licensing import LicenseIssueRequest, LicenseIssueResponse, LicenseRenewRequest


class PlatformLoginRequest(BaseModel):
    secret: str = Field(..., min_length=6)


class PlatformLoginResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    expires_hours: int = 0  # 0 = session token does not expire (platform access code never expires)


class PlatformDashboard(BaseModel):
    companies_total: int
    companies_active: int
    users_total: int
    users_active: int
    licenses_active: int
    licenses_pending: int
    deployment_nodes_online: int
    active_sessions: int = 0
    app_version: Optional[str] = None
    deployment_mode: str


class PlatformActiveSessionRow(BaseModel):
    id: str
    user_id: str
    username: str
    role: Optional[str] = None
    company_id: Optional[str] = None
    company_code: Optional[str] = None
    company_name: Optional[str] = None
    branch_id: Optional[str] = None
    branch_code: Optional[str] = None
    branch_name: Optional[str] = None
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    client_hint: Optional[str] = None
    login_method: Optional[str] = None
    session_start: datetime
    last_activity: datetime
    minutes_idle: float = 0
    cnp_support: bool = False


class PlatformBranchSummary(BaseModel):
    id: str
    code: str
    name: str
    is_head_office: bool = False
    active: bool = True


class PlatformCompanyRow(BaseModel):
    id: str
    name: str
    company_code: Optional[str]
    email: Optional[str]
    is_active: bool
    is_setup_complete: bool
    deployment_mode: Optional[str]
    license_expires_at: Optional[datetime]
    license_status: Optional[str]
    license_record_status: Optional[str] = None
    license_key_id: Optional[str] = None
    license_key_prefix: Optional[str]
    license_key: Optional[str] = None
    license_plan: Optional[str] = None
    license_max_branches: Optional[int] = None
    license_max_users: Optional[int] = None
    has_license: bool = False
    user_count: int
    branch_count: int
    branches: List[PlatformBranchSummary] = []
    created_at: Optional[datetime]


class PlatformCompanyPatch(BaseModel):
    is_active: Optional[bool] = None
    subscription_plan: Optional[str] = None


class PlatformUserRow(BaseModel):
    id: str
    username: str
    email: Optional[str]
    role: str
    active: bool
    branch_id: Optional[str]
    branch_name: Optional[str]
    branch_code: Optional[str]
    last_login: Optional[datetime]


class PlatformResetPasswordRequest(BaseModel):
    new_password: str = Field(..., min_length=6)


class PlatformImpersonateResponse(BaseModel):
    access_token: str
    token_type: str = "bearer"
    user_id: str
    username: str
    role: str
    company_id: Optional[str]
    branch_id: Optional[str]
    branch_code: Optional[str]
    erp_login_url: str
    message: str


class PlatformLicenseRow(BaseModel):
    id: str
    key_prefix: str
    license_key: Optional[str] = None
    key_stored: bool = False
    plan: str
    status: str
    deployment_mode: str
    valid_from: datetime
    valid_until: datetime
    company_id: Optional[str] = None
    customer_name: Optional[str] = None
    customer_email: Optional[str] = None
    renewals_count: int = 0
    max_users: int = 25
    max_branches: int = 1

    class Config:
        from_attributes = True


class PlatformBranchLicenseRow(BaseModel):
    id: str
    slot_code: str
    label: Optional[str] = None
    status: str
    valid_from: datetime
    valid_until: datetime
    branch_id: Optional[str] = None
    branch_name: Optional[str] = None
    branch_code: Optional[str] = None
    notes: Optional[str] = None


class PlatformBranchOption(BaseModel):
    id: str
    name: str
    code: str
    is_head_office: bool = False
    active: bool = True


class PlatformBranchLicenseCreate(BaseModel):
    label: Optional[str] = None
    branch_id: Optional[str] = None
    notes: Optional[str] = None
    count: int = Field(1, ge=1, le=20)


class PlatformCompanyLicensing(BaseModel):
    company_id: str
    company_name: str
    company_code: Optional[str] = None
    branches_used: int
    branches_allowed: int
    main_includes_branches: int = 1
    has_main_license: bool
    main_license: Optional[PlatformLicenseRow] = None
    branch_licenses: List[PlatformBranchLicenseRow] = []
    company_branches: List[PlatformBranchOption] = []


class PlatformAssignLicenseRequest(BaseModel):
    license_id: str
