import { ComisionSource, EstadoLiquidacion, EstadoLiquidacionReclamo, Id, ItemLiquidable, LiquidacionRule, LogicOperator, TipoComision } from "@win2win/shared";

export interface LiquidacionLayoutModel {
    idLiquidacionLay: number | null,
    codCliente: string | null,
    titulo: string,
    estructura: LiquidacionRule[],
    operadorRaiz?: LogicOperator
    idSituacion: number | null
    estado: 0 | 1,
    clientes: boolean | number
}
export interface ComisionModel {
    idComision?: number | null
    codCliente?: string | null,
    fechaDesde: string,
    fechaHasta: string,
    idProducto: number | null,
    idDelegacion: number | null,
    idGerencia: number | null,
    tipoCliente: number | null,
    tipoServicio: number | null,
    items: ComisionItemModel[]
    clientes: boolean | number
}
export interface SituacionModel {
    id_situacion: number | null
    cod_cliente: string | null
    nombre: string
    descripcion: string,
    estado: 0 | 1
    clientes: boolean | number
}

export interface ComisionItemModel {
    idSituacion: number | null;
    tipo: TipoComision;
    source: ComisionSource | null;

    // montos
    cliente: number;
    organizador: number;
    gerente: number;
    promotor: number;
    over1: number;
    over2: number;
    over3: number;
}

export const COMISION_ITEM_ROLES = ['cliente', 'organizador', 'gerente', 'promotor', 'over1'] as const //, 'over1', 'over2', 'over3';

export type LiquidacionRuleModel = Omit<LiquidacionRule, 'sourceCode'> & { sourceCode: any }

export interface ItemsLiquidablesFilters {
    layout: number | null,
    delegacion: number | null,
    gerencia: number | null,
    usuario: number | null,
    fecha: {
        from: string | null,
        to: string | null
    },
    situacion: number | null,
    codCliente?: string
    clienteKey?: string
}

export interface SituacionRaw {
    ID_SITUACION: number,
    NOMBRE: string,
    DESCRIPCION: string,
    ESTADO: 0 | 1
}

export const ESTADOS_LIQUIDACION = {
    [EstadoLiquidacion.EN_PREVISION]: { label: 'En Previsión', color: 'grey' },
    [EstadoLiquidacion.CONFIRMACION_PENDIENTE]: { label: 'Confirmación pendiente', color: 'yellow' },
    [EstadoLiquidacion.CONFIRMADA]: { label: 'Confirmada', color: 'green' },
    [EstadoLiquidacion.CON_RECLAMOS]: { label: 'Con reclamos', color: 'red' },
    [EstadoLiquidacion.PAGADA]: { label: 'Liquidada', color: 'blue' },
} as const;


export const ESTADOS_LIQUIDACION_RECLAMO = {
    [EstadoLiquidacionReclamo.ACTIVO]: { label: 'Sin resolver', color: 'grey' },
    [EstadoLiquidacionReclamo.APROBADO]: { label: 'Aprobado', color: 'green' },
    [EstadoLiquidacionReclamo.NEGADO]: { label: 'Rechazado', color: 'red' },
} as const;

export interface ReclamoRaw {
    ID_LIQUIDACION_RECLAMO: number;
    ID_LIQUIDACION: number;
    ID_LIQUIDACION_ITEM: null;
    ID_PEDIDO: number;
    TIPO: number;
    ID_MOTIVO_RECLAMO: string;
    DETALLE_RECLAMO: string;
    COMENTARIO_RESOLUCION: null;
    ESTADO_RECLAMO: EstadoLiquidacionReclamo;
    ID_USUARIO: number;
    ROL_USUARIO: number;
    ID_USUARIO_RESOLUCION: null;
    FECHA_UPD: Date;
    FECHA_CREATED: Date;
    USUARIO_RECLAMO: any;
    CONTRATO_RECLAMADO: any;
    BONO: null;
}


export interface LiquidacionRaw {
    ID_LIQUIDACION: Id;
    COD_CLIENTE: string | null;
    ESTADO: number;
    FECHA_PREVISION: string | null;
    FECHA_SOLICITADO: string | null;
    FECHA_CONFIRMADO: string | null;
    FECHA_LIQUIDADO: string | null;
    PERIODO: Periodo;
    ITEMS: Item[];
    GERENCIA: Gerencia;
    DELEGACION: Delegacion;
    SITUACION: any;
    RECLAMOS: ReclamoRaw[];
    ORGANIZADOR: any;
    GERENTE: any;
}

export interface Delegacion {
    ID_DELEGACION: number;
    NOMBRE: string;
    COD_DEL_MVX: string;
    ID_CONTACTO: number;
    GAMA: {
        ID_GAMA: number;
        NOMBRE: string;
        PROPS: any;
        REQUIERE: any;
    };
}

export interface Gerencia {
    ID_GERENCIA: number;
    NOMBRE: string;
}

export interface Item {
    ID_LIQUIDACION_ITEM: Id;
    CLIENTE: number;
    ORGANIZADOR: number;
    GERENTE: number;
    SUPERVISOR: number;
    PROMOTOR: number;
    OVER1: number;
    OVER2: number;
    OVER3: number;
    OVER4: number;
    OVER5: number;
    BOLSA: number;
    PEDIDO: any;
    R_PROMOTOR: any;
    CAPTACION: any;
    FECHA_CONFIRMADO: string,
    FECHA_LIQUIDADO: string,
}

export type Periodo = { FECHA_DESDE: string, FECHA_HASTA: string };
export const roles = ['CLIENTE', 'ORGANIZADOR', 'GERENTE', 'SUPERVISOR', 'PROMOTOR', 'OVER1', 'OVER2', 'OVER3', 'OVER4', 'OVER5', 'BOLSA']
export const mainRoles = roles.slice(1, 5);
export type ItemLiquidado = Omit<ItemLiquidable, 'TIPO' | 'ESTADO' | 'comision'> & {
    confirmacion_captador: boolean
    confirmacion_gerente: boolean
}


export enum EstadoItemLiquidado {
    SIN_CONFIRMAR,
    CONFIRMADO,
}