export type Plan = "free" | "pro" | "pro_max";

export interface PlanConfig {
  name: string;
  price_monthly: number;
  price_yearly: number;
  videos_per_day: number;
  max_duration_minutes: number;
  priority_processing: boolean;
  watermark: boolean;
  download_transcript: boolean;
  api_access: boolean;
  dedicated_support: boolean;
  custom_integrations: boolean;
  description: string;
}

export const PLAN_LIMITS: Record<Plan, PlanConfig> = {
  free: {
    name: "Free",
    price_monthly: 0,
    price_yearly: 0,
    videos_per_day: 1,
    max_duration_minutes: 15,
    priority_processing: false,
    watermark: true,
    download_transcript: false,
    api_access: false,
    dedicated_support: false,
    custom_integrations: false,
    description: "Perfect for trying out ReelsCutter",
  },
  pro: {
    name: "Pro",
    price_monthly: 799,
    price_yearly: 6990,
    videos_per_day: 5,
    max_duration_minutes: 20,
    priority_processing: true,
    watermark: false,
    download_transcript: true,
    api_access: false,
    dedicated_support: false,
    custom_integrations: false,
    description: "For serious content creators",
  },
  pro_max: {
    name: "Pro Max",
    price_monthly: 0,
    price_yearly: 0,
    videos_per_day: Infinity,
    max_duration_minutes: Infinity,
    priority_processing: true,
    watermark: false,
    download_transcript: true,
    api_access: true,
    dedicated_support: true,
    custom_integrations: true,
    description: "For agencies and power users",
  },
};

export function checkPlanLimit(
  plan: Plan,
  videos_used_today: number,
  video_duration_seconds: number
): { allowed: boolean; reason?: string } {
  const limits = PLAN_LIMITS[plan];

  if (videos_used_today >= limits.videos_per_day) {
    return {
      allowed: false,
      reason: `You've reached your daily limit of ${limits.videos_per_day} video${limits.videos_per_day === 1 ? "" : "s"}. Upgrade to process more.`,
    };
  }

  const duration_minutes = video_duration_seconds / 60;
  if (duration_minutes > limits.max_duration_minutes) {
    return {
      allowed: false,
      reason: `This video (${Math.round(duration_minutes)}m) exceeds your plan's ${limits.max_duration_minutes}m limit. Upgrade to process longer videos.`,
    };
  }

  return { allowed: true };
}

export function getPlanBadgeColor(plan: Plan): string {
  switch (plan) {
    case "free":
      return "bg-zinc-800 text-zinc-300 border-zinc-700";
    case "pro":
      return "bg-violet-900/50 text-violet-300 border-violet-700/50";
    case "pro_max":
      return "bg-cyan-900/50 text-cyan-300 border-cyan-700/50";
  }
}
