/**
 * REST controller for per-tenant hook scripts (CRUD).
 *
 * Endpoints (JWT validated by Kong gateway):
 *   GET    /api/v1/agent/hooks          → List all hooks for current tenant
 *   POST   /api/v1/agent/hooks          → Create a new hook
 *   PUT    /api/v1/agent/hooks/:id      → Update an existing hook
 *   DELETE /api/v1/agent/hooks/:id      → Delete a hook
 */
import { Controller, Get, Post, Put, Delete, Body, Param, Headers, NotFoundException } from '@nestjs/common';
import { HookScriptService, CreateHookDto, UpdateHookDto } from '../../../infrastructure/services/hook-script.service';

@Controller('api/v1/agent/hooks')
export class HooksController {
  constructor(private readonly hookService: HookScriptService) {}

  @Get()
  async list(@Headers('x-tenant-id') tenantId: string) {
    return this.hookService.findAllByTenant(tenantId || 'default');
  }

  @Post()
  async create(
    @Headers('x-tenant-id') tenantId: string,
    @Body() dto: CreateHookDto,
  ) {
    return this.hookService.create(tenantId || 'default', dto);
  }

  @Put(':id')
  async update(
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
    @Body() dto: UpdateHookDto,
  ) {
    const hook = await this.hookService.update(id, tenantId || 'default', dto);
    if (!hook) {
      throw new NotFoundException('Hook not found');
    }
    return hook;
  }

  @Delete(':id')
  async remove(
    @Headers('x-tenant-id') tenantId: string,
    @Param('id') id: string,
  ) {
    const deleted = await this.hookService.delete(id, tenantId || 'default');
    if (!deleted) {
      throw new NotFoundException('Hook not found');
    }
    return { message: 'Hook deleted' };
  }
}
