import { Controller, Get, Post, Param, Body, Request, Header } from "@nestjs/common";
import { InternetCancellationService } from "./services/internet-cancellation.service.js";
import { createZodDto, ZodResponse } from "nestjs-zod";
import type { RequestWithUser } from "@bff/modules/auth/auth.types.js";

import {
  subscriptionIdParamSchema,
  simActionResponseSchema,
  internetCancellationPreviewSchema,
  internetCancelRequestSchema,
  type SimActionResponse as SubscriptionActionResponse,
} from "@customer-portal/domain/subscriptions";

// DTOs
class SubscriptionIdParamDto extends createZodDto(subscriptionIdParamSchema) {}
class InternetCancelRequestDto extends createZodDto(internetCancelRequestSchema) {}

// Response DTOs
class SimActionResponseDto extends createZodDto(simActionResponseSchema) {}
class InternetCancellationPreviewResponseDto extends createZodDto(
  internetCancellationPreviewSchema
) {}

@Controller("subscriptions")
export class InternetController {
  constructor(private readonly internetCancellationService: InternetCancellationService) {}

  /**
   * Get Internet cancellation preview (available months, service details)
   */
  @Get(":id/internet/cancellation-preview")
  @Header("Cache-Control", "private, max-age=60")
  @ZodResponse({
    description: "Get internet cancellation preview",
    type: InternetCancellationPreviewResponseDto,
  })
  async getInternetCancellationPreview(
    @Request() req: RequestWithUser,
    @Param() params: SubscriptionIdParamDto
  ) {
    const preview = await this.internetCancellationService.getCancellationPreview(
      req.user.id,
      params.id
    );
    return preview;
  }

  /**
   * Submit Internet cancellation request
   */
  @Post(":id/internet/cancel")
  @ZodResponse({ description: "Cancel internet", type: SimActionResponseDto })
  async cancelInternet(
    @Request() req: RequestWithUser,
    @Param() params: SubscriptionIdParamDto,
    @Body() body: InternetCancelRequestDto
  ): Promise<SubscriptionActionResponse> {
    await this.internetCancellationService.submitCancellation(req.user.id, params.id, body);
    return {
      message: `Internet cancellation scheduled for end of ${body.cancellationMonth}`,
    };
  }
}
