import { Controller, Get, Post, Body, Param, NotFoundException } from '@nestjs/common';
import { ClusterRepository } from '../../../infrastructure/repositories/cluster.repository';
import { Cluster } from '../../../domain/entities/cluster.entity';
import * as crypto from 'crypto';

@Controller('api/v1/inventory/clusters')
export class ClusterController {
  constructor(private readonly clusterRepository: ClusterRepository) {}

  @Get()
  async listClusters() {
    return this.clusterRepository.findAll();
  }

  @Post()
  async createCluster(@Body() body: any) {
    const cluster = new Cluster();
    cluster.id = crypto.randomUUID();
    cluster.tenantId = body.tenantId;
    cluster.name = body.name;
    cluster.type = body.type;
    cluster.environment = body.environment;
    cluster.description = body.description;
    cluster.createdAt = new Date();
    return this.clusterRepository.save(cluster);
  }

  @Get(':id')
  async getCluster(@Param('id') id: string) {
    const cluster = await this.clusterRepository.findById(id);
    if (!cluster) {
      throw new NotFoundException(`Cluster ${id} not found`);
    }
    return cluster;
  }
}
