import { AuctionBuyerPaidStatus } from '@/enums/auctionBuyerPaidStatus';
import { AuctionFormat } from '@/enums/auctionFormat';
import { AuctionStatus } from '@/enums/auctionStatus';

export interface AuctionReportSelectOption {
    id: string | null;
    name: string;
}

export const auctionReportFormatOptions: AuctionReportSelectOption[] = [
    { id: 'all', name: 'All' },
    { id: AuctionFormat.Online, name: 'Online' },
    { id: AuctionFormat.InPerson, name: 'In-Person' },
];

export const auctionReportYesNoOptions: AuctionReportSelectOption[] = [
    { id: 'all', name: 'All' },
    { id: 'yes', name: 'Yes' },
    { id: 'no', name: 'No' },
];

export const auctionReportBuyerPaidOptions: AuctionReportSelectOption[] = [
    { id: 'all', name: 'All' },
    { id: AuctionBuyerPaidStatus.Yes, name: 'Yes' },
    { id: AuctionBuyerPaidStatus.Due, name: 'Due' },
    { id: AuctionBuyerPaidStatus.No, name: 'No' },
    { id: AuctionBuyerPaidStatus.NotApplicable, name: 'N/A' },
];

const onlineAuctionReportStatusIds = [
    AuctionStatus.Published,
    AuctionStatus.Sold,
    AuctionStatus.NotSold,
    AuctionStatus.Cancelled,
    AuctionStatus.Scheduled,
] as const;

const inPersonAuctionReportStatusIds = [
    AuctionStatus.Scheduled,
    AuctionStatus.Queued,
    AuctionStatus.Published,
    AuctionStatus.Sold,
    AuctionStatus.PassedIn,
    AuctionStatus.NotSold,
    AuctionStatus.Cancelled,
] as const;

const auctionReportStatusLabelById: Record<string, string> = {
    [AuctionStatus.Scheduled]: 'Scheduled',
    [AuctionStatus.Queued]: 'Queued',
    [AuctionStatus.Published]: 'Live',
    [AuctionStatus.Sold]: 'Sold',
    [AuctionStatus.PassedIn]: 'Passed in',
    [AuctionStatus.NotSold]: 'Not sold',
    [AuctionStatus.Cancelled]: 'Cancelled',
};

const auctionReportStatusDisplayOrder = [
    AuctionStatus.Scheduled,
    AuctionStatus.Queued,
    AuctionStatus.Published,
    AuctionStatus.Sold,
    AuctionStatus.PassedIn,
    AuctionStatus.NotSold,
    AuctionStatus.Cancelled,
] as const;

function auctionReportStatusIdsForFormat(formatId: string | null | undefined): string[] {
    let statusIds: string[];

    if (formatId === AuctionFormat.Online) {
        statusIds = [...onlineAuctionReportStatusIds];
    } else if (formatId === AuctionFormat.InPerson) {
        statusIds = [...inPersonAuctionReportStatusIds];
    } else {
        statusIds = [...new Set([...onlineAuctionReportStatusIds, ...inPersonAuctionReportStatusIds])];
    }

    return auctionReportStatusDisplayOrder.filter((id) => statusIds.includes(id));
}

export function auctionReportStatusOptionsForFormat(
    formatId: string | null | undefined,
): AuctionReportSelectOption[] {
    return [
        { id: 'all', name: 'All' },
        ...auctionReportStatusIdsForFormat(formatId).map((id) => ({
            id,
            name: auctionReportStatusLabelById[id],
        })),
    ];
}

export const auctionReportStatusOptions: AuctionReportSelectOption[] =
    auctionReportStatusOptionsForFormat('all');
