import defaultValues from 'config/default-values';
import { defaultValues as defaultInsuranceValues } from 'config/tables/insurance-table';
import getFingapDataFromKP from 'process/fingap/get-kp-data';
import { GetQuoteDataInputSchema, GetQuoteDataOutputSchema } from 'process/load-kp/types';
import getPaymentsDataFromKP from 'process/payments/get-kp-data';
import getSupplierAgentsDataFromKP from 'process/supplier-agent/get-kp-values';
import { t } from '../server';

const DEFAULT_OSAGO_ROW = defaultInsuranceValues.find((x) => x.key === 'osago');
const DEFAULT_KASKO_ROW = defaultInsuranceValues.find((x) => x.key === 'kasko');

const quoteRouter = t.router({
  getData: t.procedure
    .input(GetQuoteDataInputSchema)
    .output(GetQuoteDataOutputSchema)
    .query(async ({ input }) => {
      const { values: supplierAgentsValues } = await getSupplierAgentsDataFromKP(input);
      const { values: paymentsValues, payments } = await getPaymentsDataFromKP(input);
      const { fingap, insurance: fingapInsurance } = await getFingapDataFromKP(input);

      return {
        values: {
          ...defaultValues,
          ...supplierAgentsValues,
          ...paymentsValues,
        },
        payments,
        insurance: {
          values: {
            osago: DEFAULT_OSAGO_ROW,
            kasko: DEFAULT_KASKO_ROW,
            fingap: fingapInsurance.values.fingap,
          },
        },
        fingap,
      };
    }),
});

export default quoteRouter;
