// import { BaseRepository } from "@edr/api-common";
// import { EntityRepository } from "typeorm";

// src/modules/customers/customers.repository.ts
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository, FindManyOptions, FindOptionsWhere } from "typeorm";
import { Customer } from "./entities/customer.entity";
import { CreateCustomerDto } from "./dto/create-customer.dto";
// import { UpdateCustomerDto } from "./dto/update-customer.dto";

@Injectable()
export class CustomersRepository {
  constructor(
    @InjectRepository(Customer)
    private readonly repository: Repository<Customer>,
  ) { }

  async create(dto: CreateCustomerDto): Promise<Customer> {
    const customer = this.repository.create(dto);
    return await this.repository.save(customer);
  }

  async findAll(options?: FindManyOptions<Customer>): Promise<Customer[]> {
    return await this.repository.find(options);
  }

  async findById(id: string): Promise<Customer | null> {
    return await this.repository.findOne({ where: { id } as FindOptionsWhere<Customer> });
  }

  async findByUserId(userId: string): Promise<Customer | null> {
    return await this.repository.findOne({ where: { userId } as FindOptionsWhere<Customer> });
  }

  async findByEmail(email: string): Promise<Customer | null> {
    return await this.repository.findOne({ where: { email } as FindOptionsWhere<Customer> });
  }

  async findByVatNumber(vatNumber: string): Promise<Customer | null> {
    return await this.repository.findOne({ where: { vatNumber } as FindOptionsWhere<Customer> });
  }

  async findByName(name: string): Promise<Customer[]> {
    return await this.repository
      .createQueryBuilder("customer")
      .where("customer.companyName ILIKE :name", { name: `%${name}%` })
      .getMany();
  }

  async findOneByEmailOrVat(email?: string, vatNumber?: string): Promise<Customer | null> {
    if (!email && !vatNumber) return null;

    const queryBuilder = this.repository.createQueryBuilder('customer');

    if (email && vatNumber) {
      queryBuilder.where('customer.email = :email', { email })
        .orWhere('customer.vatNumber = :vatNumber', { vatNumber });
    } else if (email) {
      queryBuilder.where('customer.email = :email', { email });
    } else if (vatNumber) {
      queryBuilder.where('customer.vatNumber = :vatNumber', { vatNumber });
    }

    return await queryBuilder.getOne();
  }

  async update(id: string, updates: Partial<Customer>): Promise<Customer | null> {
    await this.repository.update(id, updates);
    return this.findById(id);
  }

  async delete(id: string): Promise<boolean> {
    const result = await this.repository.delete(id);
    return (result.affected ?? 0) > 0;
  }

  async count(where?: any): Promise<number> {
    if (where?.createdAt) {
      const result = await this.repository
        .createQueryBuilder('customer')
        .where('customer.createdAt >= :date', { date: where.createdAt })
        .getCount();
      return result;
    }
    return await this.repository.count();
  }

  async existsByUniqueFields(email: string, vatNumber?: string): Promise<boolean> {
    const queryBuilder = this.repository.createQueryBuilder('customer')
      .where('customer.email = :email', { email });

    if (vatNumber) {
      queryBuilder.orWhere('customer.vatNumber = :vatNumber', { vatNumber });
    }

    const count = await queryBuilder.getCount();
    return count > 0;
  }

  async countWithVatNumber(): Promise<number> {
    const count = await this.repository
      .createQueryBuilder('customer')
      .where('customer.vatNumber IS NOT NULL')
      .andWhere("customer.vatNumber != ''")
      .getCount();

    return count;
  }

  getRepository(): Repository<Customer> {
    return this.repository;
  }
  softDelete(id: string): any {
    return id;
  }
}