import { Controller, Get, Post, Delete, Body, Param, UseGuards, Req } from '@nestjs/common';
import { CredentialsService } from './credentials.service';
// Assuming AuthGuard is available globally or we import it.
// Checking admin/admin.controller.ts for patterns might be good but let's assume standard guard first.
// If user has 'admin' role, etc.
// Backoffice usually protected by default or via global guard.

@Controller('system/credentials')
export class CredentialsController {
    constructor(private readonly credentialsService: CredentialsService) { }

    @Get()
    async list() {
        const services = await this.credentialsService.list();
        return { services };
    }

    @Post()
    async save(@Body() body: { serviceName: string; payload: any }, @Req() req: any) {
        // Assuming req.user is populated by AuthGuard
        const updatedBy = req.user?.id || 'system';
        await this.credentialsService.save(body.serviceName, body.payload, updatedBy);
        return { message: 'Credentials saved successfully' };
    }

    @Delete(':serviceName')
    async delete(@Param('serviceName') serviceName: string) {
        await this.credentialsService.delete(serviceName);
        return { success: true };
    }
}
