import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
} from '@nestjs/common';
import {
  PartnerCreateSchema,
  PartnerUpdateSchema,
  type PartnerCreate,
  type PartnerUpdate,
} from '@siop/shared';
import { ZodValidationPipe } from '../common/zod-validation.pipe';
import { RequirePermission } from '../permissions/require-permission.decorator';
import { PartnersService } from './partners.service';

/** Les tiers vivent sous la permission PURCHASE_ORDERS (décision R3). */
@Controller('partners')
export class PartnersController {
  constructor(private readonly partners: PartnersService) {}

  @Get()
  @RequirePermission('PURCHASE_ORDERS', 'view')
  list() {
    return this.partners.list();
  }

  @Post()
  @RequirePermission('PURCHASE_ORDERS', 'create')
  create(@Body(new ZodValidationPipe(PartnerCreateSchema)) body: PartnerCreate) {
    return this.partners.create(body);
  }

  @Patch(':id')
  @RequirePermission('PURCHASE_ORDERS', 'edit')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body(new ZodValidationPipe(PartnerUpdateSchema)) body: PartnerUpdate,
  ) {
    return this.partners.update(id, body);
  }
}
