import type { StateFileToIniParserMap } from '@app/store/types.js';
import { type IniEnabled, type IniNumberBoolean } from '@app/core/types/ini.js';
import { toBoolean, toNumber, toNumberOrNull, toNumberOrNullConvert } from '@app/core/utils/index.js';
import {
    ArrayDisk,
    ArrayDiskStatus,
    ArrayDiskType,
} from '@app/unraid-api/graph/resolvers/array/array.model.js';

type SlotStatus = 'DISK_OK';
type SlotFsStatus = 'Mounted';
type SlotFsType = 'vfat' | 'ntfs';
type SlotType = 'Flash' | 'Cache' | 'Array' | 'Parity';
type SlotColor = 'green-on';

export type IniSlot = {
    color: SlotColor;
    comment: string;
    device: string;
    exportable: IniEnabled;
    format: string;
    fsColor: string;
    fsFree: string;
    fsUsed: string;
    fsSize: string;
    fsStatus: SlotFsStatus;
    fsType: SlotFsType;
    id: string;
    idx: string;
    luksState: string;
    name: string;
    numErrors: string;
    numReads: string;
    numWrites: string;
    rotational: IniNumberBoolean;
    size: string;
    sizeSb: string;
    slots: string;
    spundown: string;
    status: SlotStatus;
    temp: string;
    type: SlotType;
    warning: string;
    critical: string;
    transport: string;
};

export type SlotsIni = IniSlot[];

export const parse: StateFileToIniParserMap['disks'] = (disksIni) =>
    Object.values(disksIni)
        .filter((slot) => slot.id)
        .map((slot) => {
            const result: ArrayDisk = {
                id: slot.id,
                device: slot.device,
                comment: slot.comment ?? null,
                exportable: toBoolean(slot.exportable),
                fsFree: toNumberOrNullConvert(slot.fsFree, {
                    startingUnit: 'KiB',
                    endUnit: 'KB',
                }),
                fsUsed: toNumberOrNullConvert(slot.fsUsed, {
                    startingUnit: 'KiB',
                    endUnit: 'KB',
                }),
                fsSize: toNumberOrNullConvert(slot.fsSize, {
                    startingUnit: 'KiB',
                    endUnit: 'KB',
                }),
                idx: toNumber(slot.idx),
                name: slot.name,
                numErrors: toNumber(slot.numErrors),
                numReads: 0,
                numWrites: 0,
                rotational: toBoolean(slot.rotational),
                size: toNumber(slot.size),
                status: ArrayDiskStatus[slot.status],
                temp: toNumberOrNull(slot.temp),
                type: slot.type ? ArrayDiskType[slot.type.toUpperCase()] : undefined,
                warning: toNumberOrNull(slot.warning),
                critical: toNumberOrNull(slot.critical),
                fsType: slot.fsType ?? null,
                format: slot.format === '-' ? null : slot.format,
                transport: slot.transport ?? null,
                isSpinning: slot.spundown ? slot.spundown === '0' : null,
            };
            // @TODO Zod Parse This
            return result;
        });
