import { compact, isArray, startCase } from 'lodash';
import moment from 'moment';
import { QTableColumn } from 'quasar';
import { Producto, ProductoESTADO, Ubicacion } from 'src/models';

export const productoTabs = [
  { icon: 'arrow_back', to: '/productos', name: 'to_products' },
  { label: 'Información general', to: 'estado', name: 'estado' },
  // { label: 'Titulares', to: 'contacto', name: 'contacto' },
  // { label: 'Documentos', to: 'documentacion', name: 'documentacion' },
  { label: 'Solicitud', to: 'solicitudes', name: 'solicitudes' },
  { label: 'Marketing', to: 'marketing', name: 'marketing' },
] as const;
Object.freeze(productoTabs);

export const product_states = [
  {
    label: 'Todos',
    value: 99,
    state: '99',
    count: 0,
    color: null,
  },
  // {
  //   label: 'Inactivos',
  //   value: 0,
  //   state: '0',
  //   count: 0,
  //   color: 'red',
  // },
  {
    label: 'En estudio',
    value: 1,
    state: ProductoESTADO.INACTIVO,
    count: 0,
    color: 'grey',
  },
  {
    label: 'Activas sin publicar',
    value: 2,
    state: ProductoESTADO.ACTIVADO,
    count: 0,
    color: 'green',
  },
  {
    label: 'Activas publicadas',
    value: 3,
    state: ProductoESTADO.PUBLICO,
    count: 0,
    color: 'blue',
  },
];
Object.freeze(productoTabs);

export const SHARED_COLUMNS: QTableColumn[] = [
  { label: '', field: 'PUBLICO', name: 'PUBLICO', align: 'center' },
  { label: 'Cod.', field: 'COD_ART_MVX', name: 'COD_ART_MVX', align: 'left', sortable: true },
  { label: 'Fecha', field: (o) => moment(o.FECHA_UPD).format('DD/MM/YYYY'), name: 'FECHA_CREADO', align: 'left', sortable: true },
  { label: 'Estado', field: 'ESTADO', name: 'ESTADO', align: 'left', sortable: true },
];

export const FINCA_COLUMNS: QTableColumn[] = [
  { label: 'Etiqueta', field: 'TAGS', name: 'TAGS', align: 'center' },
  { label: 'Cod. Sol.', field: (o) => 'por hacer', name: 'ID_CAPTACION', align: 'left' },
  { label: 'Proveedor', field: (o) => getProveedor(o), name: 'PROVEEDOR', align: 'left' },
  { label: 'Tipo de suelo', field: (o) => formatProp('tipo_de_suelo', o), name: 'TIPO_SUELO', align: 'left' },
  { label: 'Agua', field: (o) => formatProp('agua', o), name: 'AGUA', align: 'left' },
  { label: 'Superficie', field: (o) => formatProp('superficie_solar', o), name: 'SUPERFICIE', align: 'center' },
  { label: 'Ubicación', field: (o) => formatUbicacion(o.UBICACION), name: 'UBICACION', align: 'left' },
];

export const HIPOTECAS_COLUMNS: QTableColumn[] = [
  { label: 'Etiqueta', field: 'TAGS', name: 'TAGS', align: 'center' },
  { label: 'Cod. Sol.', field: (o) => 'por hacer', name: 'ID_CAPTACION', align: 'left' },
  { label: 'Ubicación', field: (o) => formatUbicacion(o.UBICACION), name: 'UBICACION', align: 'left' },
];

export const TABLE_COLUMNS = {
  Fincas: FINCA_COLUMNS,
  'hipoteca inmobiliaria': HIPOTECAS_COLUMNS,
};

function getProveedor(product: Producto) {
  const contacto = product.CONTACTOS_RE_ARTICULO.find((e) => e.ID_CONTACTO === product.PROVEEDOR?.ID_CONTACTO);
  return contacto ? startCase(contacto.CONTACTO_ARTICULO.NOMBRE.toLowerCase()) : product.ID_PROVEEDOR || ' ~ ';
}

function formatProp(code: string, product: Producto) {
  const prop = product.PROPS[code];
  const gamaProp = product.R_GAMA.PROPS.filter((f) => f.code == code)[0];

  let value = prop || '~';
  if (gamaProp.controlType == 'select') {
    if (isArray(prop)) {
      let values: string[] = [];
      for (let p of prop) {
        const option = gamaProp.options?.filter((option) => option.value == p)[0];
        if (option?.label) values.push(option.label);
      }
      value = values.join(', ') || '~';
    } else {
      const option = gamaProp.options?.filter((option) => option.value == prop)[0];
      value = option?.label || '~';
    }
  }

  if (gamaProp && gamaProp.suffix) {
    value = value + ' ' + gamaProp.suffix;
  }

  return value;
}

function formatUbicacion(location: Ubicacion) {
  if (!location) return '';
  return compact([location.CIUDAD, location.PROVINCIA])
    .map((el) => el.trim())
    .join(', ');
}
