import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  Param,
  ParseIntPipe,
  Patch,
  Post,
} from '@nestjs/common';
import { createZodDto } from 'nestjs-zod';
import {
  UpsertDescontoBodySchema,
  CreatePromocaoBodySchema,
  UpdatePromocaoBodySchema,
  CreateRegraDescontoBodySchema,
  UpdateRegraDescontoBodySchema,
  type UpsertDescontoBody,
  type CreatePromocaoBody,
  type UpdatePromocaoBody,
  type CreateRegraDescontoBody,
  type UpdateRegraDescontoBody,
  type AlcadaDescontosResponse,
  type PromocoesResponse,
  type PromocoesVigentesResponse,
  type AlcadaRepResponse,
  type RegrasDescontoResponse,
  type RegrasVigentesResponse,
  type GruposProdutoResponse,
} from '@sar/api-interface';
import { PoliticasService } from './politicas.service';

class UpsertDescontoDto extends createZodDto(UpsertDescontoBodySchema) {}
class CreatePromocaoDto extends createZodDto(CreatePromocaoBodySchema) {}
class UpdatePromocaoDto extends createZodDto(UpdatePromocaoBodySchema) {}
class CreateRegraDescontoDto extends createZodDto(CreateRegraDescontoBodySchema) {}
class UpdateRegraDescontoDto extends createZodDto(UpdateRegraDescontoBodySchema) {}

@Controller({ path: 'politicas' })
export class PoliticasController {
  constructor(private readonly politicas: PoliticasService) {}

  @Get('descontos')
  listDescontos(): Promise<AlcadaDescontosResponse> {
    return this.politicas.listDescontos();
  }

  @Post('descontos')
  @HttpCode(200)
  upsertDesconto(@Body() body: UpsertDescontoDto): Promise<void> {
    return this.politicas.upsertDesconto(body as unknown as UpsertDescontoBody);
  }

  @Get('promocoes')
  listPromocoes(): Promise<PromocoesResponse> {
    return this.politicas.listPromocoes();
  }

  @Get('promocoes/vigentes')
  listPromocoesVigentes(): Promise<PromocoesVigentesResponse> {
    return this.politicas.listPromocoesVigentes();
  }

  @Get('alcada')
  minhaAlcada(): Promise<AlcadaRepResponse> {
    return this.politicas.minhaAlcada();
  }

  @Post('promocoes')
  createPromocao(@Body() body: CreatePromocaoDto): Promise<{ id: number }> {
    return this.politicas.createPromocao(body as unknown as CreatePromocaoBody);
  }

  @Patch('promocoes/:id')
  updatePromocao(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: UpdatePromocaoDto,
  ): Promise<void> {
    return this.politicas.updatePromocao(id, body as unknown as UpdatePromocaoBody);
  }

  @Delete('promocoes/:id')
  @HttpCode(204)
  deletePromocao(@Param('id', ParseIntPipe) id: number): Promise<void> {
    return this.politicas.deletePromocao(id);
  }

  @Get('regras')
  listRegras(): Promise<RegrasDescontoResponse> {
    return this.politicas.listRegras();
  }

  @Get('regras/vigentes')
  listRegrasVigentes(): Promise<RegrasVigentesResponse> {
    return this.politicas.listRegrasVigentes();
  }

  @Post('regras')
  createRegra(@Body() body: CreateRegraDescontoDto): Promise<{ id: number }> {
    return this.politicas.createRegra(body as unknown as CreateRegraDescontoBody);
  }

  @Patch('regras/:id')
  updateRegra(
    @Param('id', ParseIntPipe) id: number,
    @Body() body: UpdateRegraDescontoDto,
  ): Promise<void> {
    return this.politicas.updateRegra(id, body as unknown as UpdateRegraDescontoBody);
  }

  @Delete('regras/:id')
  @HttpCode(204)
  deleteRegra(@Param('id', ParseIntPipe) id: number): Promise<void> {
    return this.politicas.deleteRegra(id);
  }

  @Get('grupos-produto')
  listGruposProduto(): Promise<GruposProdutoResponse> {
    return this.politicas.listGruposProduto();
  }
}
