import {
  Injectable,
  ConflictException,
  NotFoundException,
} from '@nestjs/common';
import { SupplierRepository } from './repositories/supplier.repository';
import { CreateSupplierDto, UpdateSupplierDto } from './dto';
import { serviceMessages } from '../../common/constants/messages';

const MSG = serviceMessages('Supplier');

@Injectable()
export class SupplierService {
  constructor(private readonly repo: SupplierRepository) {}

  async findAll(query?: { page?: number; limit?: number; search?: string }, filters?: { destinationId?: string; supplierType?: string }) {
    const { Like } = await import('typeorm');
    const where: any = {};
    if (filters?.destinationId) where.destination_id = filters.destinationId;
    // supplier_types is simple-json (serialized array) — use Like to match within JSON string
    if (filters?.supplierType) where.supplier_types = Like(`%"${filters.supplierType}"%`);

    return this.repo.findPaginated({
      page: query?.page,
      limit: query?.limit,
      search: query?.search,
      searchColumns: ['name', 'contact_person', 'phone', 'email'],
      relations: ['destination'],
      order: { name: 'ASC' },
      where,
    });
  }

  async findAllForLookup() {
    return this.repo.find({
      where: { is_active: true } as any,
      select: ['id', 'name', 'supplier_types', 'destination_id'],
      order: { name: 'ASC' } as any,
    });
  }

  async findById(id: string) {
    const item = await this.repo.findByIdWithDestination(id);
    if (!item) throw new NotFoundException(MSG.NOT_FOUND);
    return item;
  }

  async create(dto: CreateSupplierDto) {
    const existing = await this.repo.findByNameAndDestination(dto.name, dto.destination_id);
    if (existing) throw new ConflictException(MSG.ALREADY_EXISTS('name'));

    const item = await this.repo.save({
      destination_id: dto.destination_id,
      supplier_types: dto.supplier_types,
      name: dto.name,
      contact_person: dto.contact_person,
      phone: dto.phone,
      email: dto.email || null,
      alt_phone: dto.alt_phone || null,
      photo_url: dto.photo_url || null,
      address: dto.address || null,
      // Transport per-type fields
      linked_transport_services: dto.linked_transport_services || null,
      transport_operational_area: dto.transport_operational_area || null,
      transport_operational_hours: dto.transport_operational_hours || null,
      fleet_count: dto.fleet_count ?? null,
      transport_notes: dto.transport_notes || null,
      // Activity per-type fields
      linked_activities: dto.linked_activities || null,
      activity_operational_area: dto.activity_operational_area || null,
      activity_operational_hours: dto.activity_operational_hours || null,
      capacity: dto.capacity ?? null,
      pricing_type: dto.pricing_type || null,
      activity_notes: dto.activity_notes || null,
      is_active: dto.is_active ?? true,
    });

    return { id: item.id, message: MSG.CREATED };
  }

  async update(id: string, dto: UpdateSupplierDto) {
    const item = await this.repo.findById(id);
    if (!item) throw new NotFoundException(MSG.NOT_FOUND);

    if (dto.name || dto.destination_id) {
      const checkName = dto.name || item.name;
      const checkDest = dto.destination_id || item.destination_id;
      if (checkName !== item.name || checkDest !== item.destination_id) {
        const existing = await this.repo.findByNameAndDestination(checkName, checkDest);
        if (existing && existing.id !== id) throw new ConflictException(MSG.ALREADY_EXISTS('name'));
      }
    }

    const updateData: any = {};
    const fields = [
      'destination_id', 'supplier_types', 'name', 'contact_person', 'phone',
      'email', 'alt_phone', 'photo_url', 'address',
      'linked_transport_services', 'transport_operational_area', 'transport_operational_hours', 'fleet_count', 'transport_notes',
      'linked_activities', 'activity_operational_area', 'activity_operational_hours', 'capacity', 'pricing_type', 'activity_notes',
      'is_active',
    ];
    for (const field of fields) {
      if ((dto as any)[field] !== undefined) updateData[field] = (dto as any)[field];
    }

    await this.repo.update(id, updateData);
    return { id, message: MSG.UPDATED };
  }

  async remove(id: string) {
    const item = await this.repo.findById(id);
    if (!item) throw new NotFoundException(MSG.NOT_FOUND);

    await this.repo.softDelete(id);
    return { message: MSG.DELETED };
  }
}
