// ===== 구독 상태 타입 =====
export type SubscriptionStatus = 'active' | 'pending' | 'expired' | 'cancelled' | 'suspended';

// ===== 플랜 타입 =====
export type PlanType = 'free' | 'basic' | 'premium' | 'enterprise';

// ===== API 응답 타입: 구독 정보 =====
export interface SubscriptionApiData {
  id: number;
  tenant_id: number;
  plan_id: number;
  started_at: string | null;
  ended_at: string | null;
  status: string;
  auto_renew: boolean;
  created_at: string;
  updated_at: string;
  plan?: {
    id: number;
    name: string;
    code: string;
    price: number | string;
    billing_cycle: string;
  };
  payments?: Array<{
    id: number;
    amount: number;
    status: string;
    paid_at: string | null;
  }>;
}

// ===== API 응답 타입: 사용량 (ai_tokens 포함, api_calls 제거) =====
export interface UsageApiData {
  users?: {
    used: number;
    limit: number;
    percentage: number;
  };
  storage?: {
    used: number;
    used_formatted: string;
    limit: number;
    limit_formatted: string;
    percentage: number;
  };
  ai_tokens?: {
    period: string;
    total_requests: number;
    total_tokens: number;
    prompt_tokens: number;
    completion_tokens: number;
    limit: number;
    percentage: number;
    cost_usd: number;
    cost_krw: number;
    warning_threshold: number;
    is_over_limit: boolean;
    by_model: AiTokenByModel[];
  };
  subscription?: {
    plan: string | null;
    monthly_fee: number;
    status: string;
    started_at: string | null;
    ended_at: string | null;
    remaining_days: number | null;
  };
}

// ===== AI 토큰 모델별 사용량 =====
export interface AiTokenByModel {
  model: string;
  requests: number;
  total_tokens: number;
  cost_krw: number;
}

// ===== 플랜 코드 → 타입 변환 =====
export function mapPlanCodeToType(code: string | null): PlanType {
  if (!code) return 'free';
  const mapping: Record<string, PlanType> = {
    free: 'free',
    basic: 'basic',
    premium: 'premium',
    enterprise: 'enterprise',
  };
  return mapping[code.toLowerCase()] || 'free';
}

// ===== Frontend 타입 =====
export interface SubscriptionInfo {
  // 구독 ID (취소 시 필요)
  id?: number;

  // 구독 플랜 정보
  plan: PlanType;
  planName: string;
  monthlyFee: number;
  status: SubscriptionStatus;
  startedAt: string | null;
  endedAt: string | null;
  remainingDays: number | null;

  // 사용자
  userCount: number;
  userLimit: number | null;

  // 저장공간
  storageUsed: number;
  storageLimit: number;
  storageUsedFormatted: string;
  storageLimitFormatted: string;
  storagePercentage: number;

  // AI 토큰
  aiTokens: {
    period: string;
    totalTokens: number;
    limit: number;
    percentage: number;
    costKrw: number;
    warningThreshold: number;
    isOverLimit: boolean;
    byModel: AiTokenByModel[];
  };
}

export const PLAN_LABELS: Record<SubscriptionInfo['plan'], string> = {
  free: '무료',
  basic: '베이직',
  premium: '프리미엄',
  enterprise: '엔터프라이즈',
};

export const SUBSCRIPTION_STATUS_LABELS: Record<SubscriptionStatus, string> = {
  active: '활성',
  pending: '대기 중',
  expired: '만료됨',
  cancelled: '해지됨',
  suspended: '일시정지',
};

export const PLAN_COLORS: Record<SubscriptionInfo['plan'], string> = {
  free: 'bg-gray-100 text-gray-800',
  basic: 'bg-blue-100 text-blue-800',
  premium: 'bg-purple-100 text-purple-800',
  enterprise: 'bg-amber-100 text-amber-800',
};
