import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { BaseRepository } from '@edr/api-common';
import { DeepPartial, Repository } from 'typeorm';
import { Vendor } from './entities/vendor.entity';
import { AssetAcquisition } from './entities/asset-acquisition.entity';
import { AssetDisposal } from './entities/asset-disposal.entity';

@Injectable()
export class ProcurementRepository extends BaseRepository<AssetAcquisition> {
  constructor(
    @InjectRepository(AssetAcquisition)
    private readonly acquisitionRepository: Repository<AssetAcquisition>,
    @InjectRepository(Vendor)
    private readonly vendorRepository: Repository<Vendor>,
    @InjectRepository(AssetDisposal)
    private readonly disposalRepository: Repository<AssetDisposal>,
  ) {
    super(acquisitionRepository);
  }

  // ---- Vendors ----
  async createVendor(data: DeepPartial<Vendor>): Promise<Vendor> {
    const vendor = this.vendorRepository.create(data);
    return this.vendorRepository.save(vendor);
  }

  async findVendors(): Promise<Vendor[]> {
    return this.vendorRepository.find({ order: { createdAt: 'DESC' } });
  }

  async updateVendor(id: string, data: DeepPartial<Vendor>): Promise<Vendor | null> {
    await this.vendorRepository.update(id, data as never);
    return this.vendorRepository.findOneBy({ id });
  }

  async softDeleteVendor(id: string): Promise<void> {
    await this.vendorRepository.softDelete(id);
  }

  // ---- Acquisitions ----
  async createAcquisition(data: DeepPartial<AssetAcquisition>): Promise<AssetAcquisition> {
    const acquisition = this.acquisitionRepository.create(data);
    return this.acquisitionRepository.save(acquisition);
  }

  async findAcquisitions(vehicleId?: string): Promise<AssetAcquisition[]> {
    return this.acquisitionRepository.find({
      where: vehicleId ? { vehicleId } : {},
      relations: ['vehicle', 'vendor'],
      order: { acquisitionDate: 'DESC' },
    });
  }

  async findAcquisitionById(id: string): Promise<AssetAcquisition | null> {
    return this.acquisitionRepository.findOne({
      where: { id },
      relations: ['vehicle', 'vendor'],
    });
  }

  async updateAcquisition(
    id: string,
    data: DeepPartial<AssetAcquisition>,
  ): Promise<AssetAcquisition | null> {
    await this.acquisitionRepository.update(id, data as never);
    return this.findAcquisitionById(id);
  }

  async softDeleteAcquisition(id: string): Promise<void> {
    await this.acquisitionRepository.softDelete(id);
  }

  async findLatestAcquisitionByVehicle(vehicleId: string): Promise<AssetAcquisition | null> {
    return this.acquisitionRepository.findOne({
      where: { vehicleId },
      relations: ['vehicle', 'vendor'],
      order: { acquisitionDate: 'DESC' },
    });
  }

  // ---- Disposals ----
  async createDisposal(data: DeepPartial<AssetDisposal>): Promise<AssetDisposal> {
    const disposal = this.disposalRepository.create(data);
    return this.disposalRepository.save(disposal);
  }

  async findDisposals(): Promise<AssetDisposal[]> {
    return this.disposalRepository.find({ order: { disposalDate: 'DESC' } });
  }

  async softDeleteDisposal(id: string): Promise<void> {
    await this.disposalRepository.softDelete(id);
  }

  async findLatestDisposalByVehicle(vehicleId: string): Promise<AssetDisposal | null> {
    return this.disposalRepository.findOne({
      where: { vehicleId },
      order: { disposalDate: 'DESC' },
    });
  }
}
