import { Controller, Get, Post, Body, Param } from '@nestjs/common';
import { PlansService } from './plans.service';
import { Public } from '../auth/public.decorator';

@Controller('plans')
export class PlansController {
  constructor(private readonly plansService: PlansService) {}

  @Public()
  @Get('list')
  async getPlans() {
    const plans = await this.plansService.findAllPlans();
    return { plans };
  }

  @Public()
  @Get('packages')
  async getPackages() {
    const packages = await this.plansService.findAllPackages();
    return { packages };
  }

  // --- Plans CRUD ---

  @Public()
  @Post('create')
  async createPlan(@Body() body: any) {
    const plan = await this.plansService.createPlan(body);
    return { plan };
  }

  @Public()
  @Post('update/:code')
  async updatePlan(@Param('code') code: string, @Body() body: any) {
    const plan = await this.plansService.updatePlan(code, body);
    return { plan };
  }

  @Public()
  @Post('delete/:code') // Using POST for delete to avoid CORS preflight issues sometimes, or just preference. Standard is DELETE.
  async deletePlan(@Param('code') code: string) {
    await this.plansService.deletePlan(code);
    return { success: true };
  }

  // --- Packages CRUD ---

  @Public()
  @Post('packages/create')
  async createPackage(@Body() body: any) {
    const pkg = await this.plansService.createPackage(body);
    return { package: pkg };
  }

  @Public()
  @Post('packages/update/:code')
  async updatePackage(@Param('code') code: string, @Body() body: any) {
    const pkg = await this.plansService.updatePackage(code, body);
    return { package: pkg };
  }

  @Public()
  @Post('packages/delete/:code')
  async deletePackage(@Param('code') code: string) {
    await this.plansService.deletePackage(code);
    return { success: true };
  }
}
