import { GamaProp, GamaPropControlCondition, TipoIVA, Uuid } from '@win2win/shared';
import { fromPairs } from 'lodash';
import { Archivos, CaptacionRaw, ContactoReArticulo, Ubicacion } from '.';

export interface ProductoDetallePack {
  ID_PRODUCTO_DETALLE_PACK?: number;
  ID_PRODUCTO_PACK?: number;
  ID_PRODUCTO_DETALLE?: number;
  CANTIDAD?: number;
  PRINCIPAL?: boolean;
  PACK?: Producto;
  DETALLE?: Producto;
  DESCUENTO?: number;
  PROPS: ProductoProps;
}

export interface Producto {
  ID_PRODUCTO: number;
  ID_EMPRESA: number;
  COD_ART_MVX: string;
  NOMBRE: string | null;
  DESCRIPCION: string | null;
  ID_CAPTACION: number;
  ID_PROVEEDOR: number;
  TIPO: string;
  ESTADO: ProductoESTADO;
  ID_GAMA: number | null;
  PROPS: ProductoProps;
  FECHA_UPD: Date;
  FECHA_CREADO: Date;
  EMPRESA: Empresa;
  PROVEEDOR: Proveedor;
  DESCRIPCION_CORTA: string;
  CAPTACIONES: CaptacionRaw[];
  CAPTACION: CaptacionRaw;
  R_GAMA: RGama;
  TIPO_DATA: TipoData;
  R_ARCHIVOS_ARTICULO: Archivos[];
  UBICACION: Ubicacion;
  CONTACTOS_RE_ARTICULO: ContactoReArticulo[];
  ID_TIE_DEF: null;
  ART_REF: any[];
  ART_PROPS: any[];
  TIPO_SERVICIO: any[];
  INCIDENCIAS: any[];
  GRUPO: string;
  UNDBASICA: null;
  FACTORAGR: null;
  CONTROLLOTE: null;
  TIPO_CLIENTE: null;
  TARIFA: null;
  TIE_DEF: null;
  PUBLICO: boolean;
  TAGS: string; // array separado por comas
  ID_PAQUETE?: number;
  ID_PRODUCTOS_PAQUETE: string;
  PAQUETE: boolean;
  PACK?: boolean;
  DETALLE_PACK?: ProductoDetallePack[];
  PRODUCTO_PACK?: ProductoDetallePack[];
  PRECIO: number;
  PRECIOS?: Array<{ ID_PAQUETE?: number; PRECIO: number }>;
  PROVEEDORES?: any[];

  ID_ESTADO: Uuid;
  TIPO_IVA: TipoIVA;
  R_IMG: Archivos[];
  FECHA_ALTA: string | null;
}

export interface ContactoArticulo {
  ID_CONTACTO: number;
  COD_EMP_MVX: number;
  ID_LANDING: number;
  ID_DIRECCION_FACTURACION: null;
  TIPO_DE_PERSONA: string;
  TIPOS: null;
  RAZON_SOCIAL: null;
  NOMBRE: string;
  APELLIDO: null;
  APELLIDO2: null;
  PUBLIC_TOKEN: string;
  PERSONA_CONTACTO: null;
  ID_CLIENTE: null;
  NIF: null;
  NUMERO_DOC: null;
  TIPO_DOC: null;
  COD_POSTAL: null;
  PAGINA_WEB: null;
  POBLACION: null;
  EXTRANJERO: null;
  PASAPORTE: null;
  CURP: null;
  RFC: null;
  PERMISO: null;
  FM: null;
  EMAIL: string;
  ESTADO: string;
  ESTADO_DOCUMENTOS: number;
  ESTADO_DIRECCION: number;
  COMENTARIO: null;
  FECHA_NACIMIENTO: null;
  FECHA_UPD: Date;
  FECHA_CREADO: Date;
  CONTACTO_ARTICULO_TELEFONO: ContactoArticuloTelefono;
  CONTACTO_ARTICULO_ARCHIVOS: any[];
}

export interface ContactoArticuloTelefono {
  ID_CONTACTO_TEL: number;
  ID_CONTACTO: number;
  NUMERO: string;
  NOMBRE: string;
  ESTADO: number;
}

export interface Empresa {
  ID_EMPRESA: number;
  NOMBRE_EMP: string;
  ID_DIVISION: null;
  COD_EMP_MVX: number;
  ESTADO: number;
  AI: AI;
  FECHA_UPD: Date;
}

export interface AI {
  ALMACENES: number;
  PRODUCTOS: number;
}

export type ProductoProps = {
  [key: string]: any;
};

export interface Proveedor {
  ID_PROVEEDOR: number;
  COD_EMP_MVX: number;
  COD_PROVEEDOR: string;
  CONTACTO: {
    ID_CONTACTO: number;
    RAZON_SOCIAL: string;
    TELEFONO: {
      NUMERO: string;
    };
  };
  ID_CONTACTO: number;
  ID_ALMACEN_PRED: null;
  FECHA_UPD: Date;
  FECHA_CREATE: Date;
}

export interface RArchivosArticuloDocumento {
  ID_TIPO_DOCUMENTO: number;
  NOMBRE: string;
  DESCRIPCION: string;
  ESTADO: number;
}

export interface RGama {
  ID_GAMA: number;
  ID_GAMA_PADRE: number;
  ID_EMPRESA: number;
  CODIGO: number;
  NOMBRE: string;
  ESTADO: string;
  MOSTRAR: string;
  PROPS: GamaProp[];
  REQUIERE: GamaRequirements;
  FECHA_CREADO: Date;
  FECHA_UPD: Date;
  TAGS: string; // array separado por comas
}

export interface GamaRequirements {
  DOCUMENTOS: string;
  SOLICITUD: boolean;
  INVERSION: boolean;
  // 'LLAMADA_PREVENTA',
  // 'LLAMADA_POSTVENTA',
  // 'SOLICITUD_CON_PRODUCTO'
  // 'PAGO',
  // 'ENVIO',
}

export interface TipoData {
  ID_PARAM: number;
  ID_EMPRESA: number;
  CLAVE: string;
  TIPO: number;
  VALOR: string;
  ESTADO: number;
  FLAG1: null;
  FECHA_UPD: Date;
  FECHA_CREATE: Date;
}

export enum ProductoESTADO {
  DESHABILITADO = '0',
  INACTIVO = '1',
  ACTIVADO = '2',
  PUBLICO = '3',
}

export enum ProductoTIPO {
  ARTICULO = '1',
  SERVICIO_CONTRATO = '2',
  MEMBRESIA = '3',
  OFERTA_EMPLEO = '4',
}

export interface ProductPropImg {
  file?: object;
  src?: string;
}

export interface ProductFilter {
  bycol?: object;
  date?: string | object;
}

export interface ProductoStat {
  label: string;
  value: string | number | null | undefined;
  count: string | number | null | undefined;
  type?: string;
}

export const DEFAULT_PROP: Partial<GamaProp> = {
  code: '',
  label: '',
  controlType: 'input_text',
  multiple: false,
  suffix: '',
  prefix: '',
  image: null,
  options: [],
  visibility: {
    summary: true,
    detail: true,
    form: true,
    public: false,
    roles: [],
  },
  validators: [],
  structure: [],
  // conditions: [],
};
Object.freeze(DEFAULT_PROP);

export const DEFAULT_CONTROL_CONDITION: GamaPropControlCondition = {
  id: null,
  logic: 'AND',
  children: [],
  operator: '==',
  value: '',
};
Object.freeze(DEFAULT_CONTROL_CONDITION);

export enum GamaNames {
  FINCAS = 'Fincas',
  HIPOTECAS = 'Hipotecas',
}

export const getPropValueType = (prop?: GamaProp | GamaProp[]) => {
  if (!prop) return '';
  if (Array.isArray(prop)) {
    return fromPairs(prop.map((p) => [p.code, getPropValueType(p)]));
  } else {
    return {
      input_text: 'string',
      input_number: 'number',
      switch: 'boolean',
      checkbox: 'boolean',
      date: 'Date',
      time: 'Date',
      datetime: 'Date',
      select: 'string' + (prop.multiple ? '[]' : ''),
      cards_select: 'string' + (prop.multiple ? '[]' : ''),
      dynamic_array: getPropValueType(prop.structure || []),
      object: getPropValueType(prop.structure || []),
    }[prop.controlType];
  }
};

export enum RProdCampaniaTIPO_VINCULO {
  PERTENENCIA = '1',
  PERMISO_VENTA = '2',
}
