from __future__ import annotations

import enum
from typing import TYPE_CHECKING

import attrs

if TYPE_CHECKING:
    from datetime import datetime


class PlanType(enum.StrEnum):
    FREE = "FREE"
    PRO = "PRO"
    ENTERPRISE = "ENTERPRISE"


class SubscriptionStatus(enum.StrEnum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    CANCELED = "canceled"
    PAST_DUE = "past_due"


SUBSCRIPTION_PLANS: dict[str, dict[str, object]] = {
    "FREE": {"name": "Free", "optimizations": 4000, "price": 0},
    "PRO": {
        "name": "Pro",
        "optimizations": 100_000,
        "monthly_price": 3000,
        "yearly_price": 30_000,
    },
    "ENTERPRISE": {
        "name": "Enterprise",
        "optimizations": None,
        "price": None,
    },
}

CODEFLASH_EMPLOYEE_IDS: frozenset[str] = frozenset(
    {
        "github|1271289",
        "github|10488227",
        "github|64513301",
        "github|106575910",
        "github|206515457",
        "github|4725571",
        "github|235426847",
    }
)


def is_codeflash_employee(user_id: str) -> bool:
    """
    Check if *user_id* belongs to a Codeflash employee.
    """
    return user_id in CODEFLASH_EMPLOYEE_IDS


@attrs.frozen
class APIKey:
    """
    A hashed API key record from the ``cf_api_keys`` table.
    """

    id: int
    key: str
    suffix: str
    name: str
    user_id: str | None
    tier: str | None
    organization_id: str | None
    created_at: datetime
    last_used: datetime | None


@attrs.frozen
class User:
    """
    A user record from the ``users`` table.
    """

    user_id: str
    github_username: str
    email: str | None = None
    name: str | None = None
    onboarding_completed: bool = False
    created_at: datetime | None = None


@attrs.frozen
class Organization:
    """
    An organization record from the ``organizations`` table.
    """

    id: str
    name: str
    github_org_id: str | None = None
    description: str | None = None
    website: str | None = None
    auto_add_github_members: bool = True
    added_by: str | None = None
    privacy_mode: bool = False
    subscription: bool = False
    created_at: datetime | None = None
    updated_at: datetime | None = None


@attrs.frozen
class Subscription:
    """
    A subscription record from the ``subscriptions`` table.
    """

    id: str
    user_id: str
    plan_type: str
    optimizations_used: int = 0
    total_lifetime_optimizations: int = 0
    optimizations_limit: int = 4000
    subscription_status: str = "active"
    stripe_customer_id: str | None = None
    stripe_subscription_id: str | None = None
    current_period_start: datetime | None = None
    current_period_end: datetime | None = None
    cancel_at_period_end: bool = False
    cancellation_request_date: datetime | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None


@attrs.frozen
class AuthenticatedUser:
    """
    The authenticated identity attached to each request.
    """

    user_id: str
    tier: str | None
    api_key_id: int
    organization_id: str | None
    should_log_features: bool


@attrs.frozen
class SubscriptionInfo:
    """
    Usage info attached to each metered request.
    """

    user_id: str
    tier: str
    used: int
    limit: int | None
    organization_id: str | None = None
