export type ServicioContratoKey =
    | 'cuotaFija'
    | 'proteccionPagos'
    | 'servicioBateriaVirtual'
    | 'servicioBasico'
    | 'servicioElectrodomestico'
    | 'servicioPremium'
    | 'servicioUrgencias';

export interface ServicioContratoField {
    key: ServicioContratoKey;
    label: string;
    abbrev: string;
    defaultValue: boolean;
}

export const SERVICIOS_CONTRATO_FIELDS: ServicioContratoField[] = [
    { key: 'cuotaFija', label: 'Cuota Fija', abbrev: 'CF', defaultValue: false },
    { key: 'proteccionPagos', label: 'Protección Pagos', abbrev: 'PP', defaultValue: false },
    { key: 'servicioBateriaVirtual', label: 'Servicio Batería Virtual', abbrev: 'SBV', defaultValue: false },
    { key: 'servicioBasico', label: 'Servicio Básico', abbrev: 'SBas', defaultValue: true },
    { key: 'servicioElectrodomestico', label: 'Servicio Electrodoméstico', abbrev: 'SE', defaultValue: false },
    { key: 'servicioPremium', label: 'Servicio Premium', abbrev: 'SP', defaultValue: false },
    { key: 'servicioUrgencias', label: 'Servicio Urgencias', abbrev: 'SU', defaultValue: false },
];

export const INITIAL_SERVICIOS_CONTRATO_VALUES: Record<ServicioContratoKey, boolean> =
    SERVICIOS_CONTRATO_FIELDS.reduce((acc, field) => {
        acc[field.key] = field.defaultValue;
        return acc;
    }, {} as Record<ServicioContratoKey, boolean>);

export function isServicioContratoChecked(value: unknown): boolean {
    return value === true || value === 1 || value === '1';
}

/** Valores '0'/'1' para filas de tabla o datos comunes en carga masiva */
export function serviciosContratoAsTableRow(
    row?: Partial<Record<ServicioContratoKey, unknown>>,
): Record<ServicioContratoKey, string> {
    return SERVICIOS_CONTRATO_FIELDS.reduce((acc, field) => {
        const raw = row?.[field.key];
        if (raw === undefined || raw === null || raw === '') {
            acc[field.key] = field.defaultValue ? '1' : '0';
        } else {
            acc[field.key] = isServicioContratoChecked(raw) ? '1' : '0';
        }
        return acc;
    }, {} as Record<ServicioContratoKey, string>);
}
