import { BaseApiService } from '@/services/api/base';
import { BaseApiResponse } from '@/types/api/api-general';
import {
  Uniformity,
  UniformityDetail,
  VerifyUniformityPayload,
  VerifyUniformityResponse,
  CreateUniformityPayload,
} from '@/types/api/production/uniformity';

export class UniformityApiService extends BaseApiService<
  Uniformity,
  CreateUniformityPayload,
  VerifyUniformityPayload
> {
  constructor(basePath: string) {
    super(basePath);
  }

  async getUniformity(
    project_flock_kandang_id?: number,
    start_date?: string,
    end_date?: string,
    page?: number,
    limit?: number
  ): Promise<BaseApiResponse<Uniformity> | undefined> {
    return await this.customRequest<BaseApiResponse<Uniformity>>('', {
      method: 'GET',
      params: {
        project_flock_kandang_id: project_flock_kandang_id,
        start_date: start_date,
        end_date: end_date,
        page: page,
        limit: limit,
      },
    });
  }

  async getUniformityDetail(
    id: number,
    with_details = false
  ): Promise<BaseApiResponse<UniformityDetail> | undefined> {
    return await this.customRequest<BaseApiResponse<UniformityDetail>>(
      `/${id}`,
      {
        method: 'GET',
        params: {
          with_details: with_details,
        },
      }
    );
  }

  async createUniformity(
    payload: CreateUniformityPayload
  ): Promise<BaseApiResponse<UniformityDetail> | undefined> {
    const formData = new FormData();
    formData.append('date', payload.date);
    formData.append(
      'project_flock_kandang_id',
      payload.project_flock_kandang_id.toString()
    );

    if (payload.document) {
      formData.append('document', payload.document);
    }

    return await this.customRequest<BaseApiResponse<UniformityDetail>>('', {
      method: 'POST',
      payload: formData as unknown as Record<string, unknown>,
    });
  }

  async verifyUniformity(
    payload: VerifyUniformityPayload
  ): Promise<BaseApiResponse<VerifyUniformityResponse> | undefined> {
    const formData = new FormData();
    if (payload.document) {
      formData.append('document', payload.document);
    }

    return await this.customRequest<BaseApiResponse<VerifyUniformityResponse>>(
      '/verify',
      {
        method: 'POST',
        payload: formData as unknown as Record<string, unknown>,
      }
    );
  }

  async approve(
    idOrIds: number | number[],
    notes?: string
  ): Promise<BaseApiResponse<Uniformity[]> | undefined> {
    const approvable_ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];
    return await this.customRequest<BaseApiResponse<Uniformity[]>>(
      'approvals',
      {
        method: 'POST',
        payload: {
          action: 'APPROVED',
          approvable_ids,
          notes,
        },
      }
    );
  }

  async reject(
    idOrIds: number | number[],
    notes: string = ''
  ): Promise<BaseApiResponse<Uniformity[]> | undefined> {
    const approvable_ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];
    return await this.customRequest<BaseApiResponse<Uniformity[]>>(
      'approvals',
      {
        method: 'POST',
        payload: {
          action: 'REJECTED',
          approvable_ids,
          notes,
        },
      }
    );
  }

  async delete(id: number): Promise<BaseApiResponse<Uniformity> | undefined> {
    return await this.customRequest<BaseApiResponse<Uniformity>>(`/${id}`, {
      method: 'DELETE',
    });
  }
}

export const UniformityApi = new UniformityApiService(
  'production/uniformities'
  // 'http://localhost:4010/api/production/uniformities'
);
