import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../../prisma/prisma.service';
import { CreateLostReasonDto } from '../dto/create-lost-reason.dto';
import { UpdateLostReasonDto } from '../dto/update-lost-reason.dto';

@Injectable()
export class LostReasonsService {
  constructor(private readonly prisma: PrismaService) {}

  async findAll(clubId: string | null): Promise<any[]> {
    return this.prisma.crmLostReason.findMany({
      where: { clubId, isActive: true },
      orderBy: { position: 'asc' },
    });
  }

  async create(clubId: string | null, dto: CreateLostReasonDto): Promise<any> {
    return this.prisma.crmLostReason.create({
      data: { clubId, ...dto },
    });
  }

  async update(id: string, dto: UpdateLostReasonDto): Promise<any> {
    const reason = await this.prisma.crmLostReason.findUnique({
      where: { id },
    });
    if (!reason) throw new NotFoundException(`Lost reason ${id} not found`);
    return this.prisma.crmLostReason.update({
      where: { id },
      data: dto,
    });
  }
}
