import { BadRequestException, Controller, Get, Param, Query, Request } from '@nestjs/common';
import type { Request as ExpressRequest } from 'express';
import { AgentArtifactService } from '../artifact/artifact.service';
import { resolveOrgId } from '../utils/auth-resolution.util';

@Controller('agent/artifacts')
export class AgentArtifactController {
  constructor(private readonly artifactService: AgentArtifactService) {}

  @Get()
  async list(@Query() q: { sessionId?: string }, @Request() req: ExpressRequest) {
    if (!q.sessionId) throw new BadRequestException('sessionId required');
    const orgId = resolveOrgId(req);
    if (!orgId) throw new BadRequestException('organizationId required');
    const items = await this.artifactService.listForSession(q.sessionId, orgId);
    return { items };
  }

  @Get(':id')
  async get(@Param('id') id: string, @Request() req: ExpressRequest) {
    const orgId = resolveOrgId(req);
    if (!orgId) throw new BadRequestException('organizationId required');
    return this.artifactService.getById(id, orgId);
  }
}
