/* eslint-disable @typescript-eslint/no-explicit-any */
import { STATE_AWARD_TYPE_ID } from '@/utils/awardHelpers';

import { AwardDescriptor, AwardInstance } from '@/types/Award';

import { javaPost } from './apiClient';

interface AwardDetailsRequestBody {
  awardId: number | string | null;
  slug: string | null;
}

const defaultAwardDetailsRequestBody: AwardDetailsRequestBody = {
  awardId: null,
  slug: null,
};

/**
 * Fetches every instance (díjpéldány) of a single award, addressed by `slug`
 * or `awardId` (the endpoint accepts either). Returns a flat list of recipient
 * records that all share the same `dij` descriptor; the detail page groups
 * them by year.
 */
export async function getAwardDetails({
  body = {},
  options = {},
}: {
  body?: Partial<AwardDetailsRequestBody>;
  options?: any;
} = {}): Promise<AwardInstance[]> {
  try {
    const response = await javaPost(
      'personAwardingInstanceControl/getAwardDetails',
      { ...defaultAwardDetailsRequestBody, ...body },
      options
    );
    return Array.isArray(response) ? response : [];
  } catch (error) {
    console.error('Error fetching award details:', error);
    return [];
  }
}

interface AwardListRequestBody {
  awardType: number | string;
}

const defaultAwardListRequestBody: AwardListRequestBody = {
  awardType: STATE_AWARD_TYPE_ID, // state ("állami") awards
};

/**
 * Lists awards of a given type (defaults to state awards).
 * Not wired into any page yet.
 */
export async function getAwardList({
  body = {},
  options = {},
}: {
  body?: Partial<AwardListRequestBody>;
  options?: any;
} = {}): Promise<AwardDescriptor[]> {
  try {
    const response = await javaPost(
      'awardingControl/getAwardList',
      { ...defaultAwardListRequestBody, ...body },
      options
    );
    return Array.isArray(response) ? response : [];
  } catch (error) {
    console.error('Error fetching award list:', error);
    return [];
  }
}
