import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { IsOptional, IsString, Length } from 'class-validator';
import { SuppliersService } from '../services/suppliers.service';
import { Roles } from '../../../common/decorators/roles.decorator';
import { CurrentUser, AuthenticatedUser } from '../../../common/decorators/current-user.decorator';

class CreateSupplierDto {
  @IsString() @Length(2, 200) name!: string;
  @IsOptional() @IsString() legalId?: string;
  @IsOptional() @IsString() iban?: string;
  @IsOptional() @IsString() contact?: string;
}

class UpdateSupplierDto {
  @IsOptional() @IsString() name?: string;
  @IsOptional() @IsString() legalId?: string;
  @IsOptional() @IsString() iban?: string;
  @IsOptional() @IsString() contact?: string;
}

@ApiTags('Finance — Suppliers')
@ApiBearerAuth()
@Controller('suppliers')
export class SuppliersController {
  constructor(private readonly suppliers: SuppliersService) {}

  @Roles('SUPERADMIN', 'SYNDIC')
  @Post()
  create(@CurrentUser() user: AuthenticatedUser, @Body() dto: CreateSupplierDto) {
    return this.suppliers.create(user, dto);
  }

  @Get()
  list(@CurrentUser() user: AuthenticatedUser) {
    return this.suppliers.list(user);
  }

  @Get(':id')
  findOne(@CurrentUser() user: AuthenticatedUser, @Param('id') id: string) {
    return this.suppliers.findOne(user, id);
  }

  @Roles('SUPERADMIN', 'SYNDIC')
  @Patch(':id')
  update(
    @CurrentUser() user: AuthenticatedUser,
    @Param('id') id: string,
    @Body() dto: UpdateSupplierDto,
  ) {
    return this.suppliers.update(user, id, dto);
  }

  @Roles('SUPERADMIN', 'SYNDIC')
  @Delete(':id')
  remove(@CurrentUser() user: AuthenticatedUser, @Param('id') id: string) {
    return this.suppliers.remove(user, id);
  }
}
