import { Controller, Get, Request, UseGuards, Header } from "@nestjs/common";
import { Throttle, ThrottlerGuard } from "@nestjs/throttler";
import type { RequestWithUser } from "@bff/modules/auth/auth.types";
import {
  parseInternetCatalog,
  parseSimCatalog,
  type InternetAddonCatalogItem,
  type InternetInstallationCatalogItem,
  type InternetPlanCatalogItem,
  type SimActivationFeeCatalogItem,
  type SimCatalogCollection,
  type SimCatalogProduct,
  type VpnCatalogProduct,
} from "@customer-portal/domain/catalog";
import { InternetCatalogService } from "./services/internet-catalog.service";
import { SimCatalogService } from "./services/sim-catalog.service";
import { VpnCatalogService } from "./services/vpn-catalog.service";
import { SalesforceReadThrottleGuard } from "@bff/integrations/salesforce/guards/salesforce-read-throttle.guard";

@Controller("catalog")
@UseGuards(SalesforceReadThrottleGuard, ThrottlerGuard)
export class CatalogController {
  constructor(
    private internetCatalog: InternetCatalogService,
    private simCatalog: SimCatalogService,
    private vpnCatalog: VpnCatalogService
  ) {}

  @Get("internet/plans")
  @Throttle({ default: { limit: 20, ttl: 60 } }) // 20 requests per minute
  @Header("Cache-Control", "private, max-age=300") // Personalised responses: prevent shared caching
  async getInternetPlans(@Request() req: RequestWithUser): Promise<{
    plans: InternetPlanCatalogItem[];
    installations: InternetInstallationCatalogItem[];
    addons: InternetAddonCatalogItem[];
  }> {
    const userId = req.user?.id;
    if (!userId) {
      const catalog = await this.internetCatalog.getCatalogData();
      return parseInternetCatalog(catalog);
    }

    const [plans, installations, addons] = await Promise.all([
      this.internetCatalog.getPlansForUser(userId),
      this.internetCatalog.getInstallations(),
      this.internetCatalog.getAddons(),
    ]);

    return parseInternetCatalog({ plans, installations, addons });
  }

  @Get("internet/addons")
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getInternetAddons(): Promise<InternetAddonCatalogItem[]> {
    return this.internetCatalog.getAddons();
  }

  @Get("internet/installations")
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getInternetInstallations(): Promise<InternetInstallationCatalogItem[]> {
    return this.internetCatalog.getInstallations();
  }

  @Get("sim/plans")
  @Throttle({ default: { limit: 20, ttl: 60 } }) // 20 requests per minute
  @Header("Cache-Control", "private, max-age=300") // Personalised responses: prevent shared caching
  async getSimCatalogData(@Request() req: RequestWithUser): Promise<SimCatalogCollection> {
    const userId = req.user?.id;
    if (!userId) {
      const catalog = await this.simCatalog.getCatalogData();
      return parseSimCatalog({
        ...catalog,
        plans: catalog.plans.filter(plan => !plan.simHasFamilyDiscount),
      });
    }

    const [plans, activationFees, addons] = await Promise.all([
      this.simCatalog.getPlansForUser(userId),
      this.simCatalog.getActivationFees(),
      this.simCatalog.getAddons(),
    ]);

    return parseSimCatalog({ plans, activationFees, addons });
  }

  @Get("sim/activation-fees")
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getSimActivationFees(): Promise<SimActivationFeeCatalogItem[]> {
    return this.simCatalog.getActivationFees();
  }

  @Get("sim/addons")
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getSimAddons(): Promise<SimCatalogProduct[]> {
    return this.simCatalog.getAddons();
  }

  @Get("vpn/plans")
  @Throttle({ default: { limit: 20, ttl: 60 } }) // 20 requests per minute
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getVpnPlans(): Promise<VpnCatalogProduct[]> {
    return this.vpnCatalog.getPlans();
  }

  @Get("vpn/activation-fees")
  @Header("Cache-Control", "public, max-age=300, s-maxage=300") // 5 minutes
  async getVpnActivationFees(): Promise<VpnCatalogProduct[]> {
    return this.vpnCatalog.getActivationFees();
  }
}
