// ===== 구독 상태 타입 =====
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;
  }>;
}

export interface UsageApiData {
  subscription?: {
    remaining_days: number | null;
  };
  users?: {
    used: number;
    limit: number;
  };
  storage?: {
    used: number;
    limit: number;
    used_formatted: string;
    limit_formatted: string;
  };
  api_calls?: {
    used: number;
    limit: number;
    percentage: 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;

  // 결제 정보
  lastPaymentDate: string;
  nextPaymentDate: string;
  subscriptionAmount: number;

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

  // 사용량 정보
  userCount: number;
  userLimit: number | null; // null = 무제한
  storageUsed: number;
  storageLimit: number;
  storageUsedFormatted?: string;
  storageLimitFormatted?: string;
  apiCallsUsed?: number;
  apiCallsLimit?: number;
}

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',
};