import { Controller, Get, Post, Patch, Delete, Param, Body, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
import { PlansService, Plan } from './plans.service';

class CreatePlanDto {
  id: string;
  name: string;
  description: string;
  monthlyPrice: number;
  yearlyPrice: number;
  features: string[];
  popular?: boolean;
}

class UpdatePlanDto {
  name?: string;
  description?: string;
  monthlyPrice?: number;
  yearlyPrice?: number;
  features?: string[];
  popular?: boolean;
}

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

  @Get()
  @ApiOperation({ summary: 'Get all plans' })
  getAllPlans() {
    return this.plansService.getAllPlans();
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get plan by ID' })
  getPlanById(@Param('id') id: string) {
    const plan = this.plansService.getPlanById(id);
    if (!plan) {
      throw new NotFoundException(`Plan with ID ${id} not found`);
    }
    return plan;
  }

  @Post()
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a new plan' })
  @ApiBody({ type: CreatePlanDto })
  createPlan(@Body() body: CreatePlanDto) {
    return this.plansService.createPlan(body);
  }

  @Patch(':id')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update a plan' })
  @ApiBody({ type: UpdatePlanDto })
  updatePlan(@Param('id') id: string, @Body() body: UpdatePlanDto) {
    const plan = this.plansService.updatePlan(id, body);
    if (!plan) {
      throw new NotFoundException(`Plan with ID ${id} not found`);
    }
    return plan;
  }

  @Delete(':id')
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Delete a plan' })
  deletePlan(@Param('id') id: string) {
    const deleted = this.plansService.deletePlan(id);
    if (!deleted) {
      throw new NotFoundException(`Plan with ID ${id} not found`);
    }
    return { message: `Plan ${id} deleted successfully` };
  }
}

