import { Injectable } from '@nestjs/common';
import { PrismaService } from '@modules/shared';
import { type IInfrastructureDataService, type InfrastructureProjectData } from '../../domain/services/infrastructure-data.service';

@Injectable()
export class PrismaInfrastructureDataService implements IInfrastructureDataService {
  constructor(private readonly prisma: PrismaService) {}

  async getByProvince(province: string, category?: string): Promise<InfrastructureProjectData[]> {
    const where: Record<string, unknown> = { province };
    if (category) where['category'] = category;

    const rows = await this.prisma.infrastructureProject.findMany({
      where,
      orderBy: { createdAt: 'desc' },
    });

    return rows.map((r) => ({
      id: r.id,
      name: r.name,
      province: r.province,
      category: r.category,
      status: r.status,
      investmentVND: r.investmentVND,
      startDate: r.startDate,
      completionDate: r.completionDate,
      description: r.description,
    }));
  }

  async getByStatus(status: string): Promise<InfrastructureProjectData[]> {
    const rows = await this.prisma.infrastructureProject.findMany({
      where: { status },
      orderBy: { createdAt: 'desc' },
    });

    return rows.map((r) => ({
      id: r.id,
      name: r.name,
      province: r.province,
      category: r.category,
      status: r.status,
      investmentVND: r.investmentVND,
      startDate: r.startDate,
      completionDate: r.completionDate,
      description: r.description,
    }));
  }
}
