import SectionContainer from '../../../components/SectionContainer';
import { useEffect, useMemo, useState } from 'react';
import { DataGrid } from '@mui/x-data-grid';
import {
  Typography, Button, Dialog, DialogTitle, DialogContent,
  IconButton, Box, ToggleButtonGroup, ToggleButton
} from '@mui/material';
import EditRoundedIcon from '@mui/icons-material/EditRounded';
import DeleteRoundedIcon from '@mui/icons-material/DeleteRounded';
import AddRoundedIcon from '@mui/icons-material/AddRounded';
import AddOrEditProductCollectionForm from './AddOrEditProductCollectionForm';
import FurnitureVariantApi from '../../../api/ProductsApi';
import CategoriesApi from '../../../api/CategoriesApi';
import TagTypeApi from '../../../api/TagTypeApi';
import { useAuth } from '../../../context/AuthContext';
import useApiToast from '../../../hooks/useApiToast';

const parsePrice = (p) => {
  if (p == null) return 0;
  if (typeof p === 'number') return p;
  if (typeof p === 'string') return Number(p) || 0;
  if (typeof p === 'object' && p.$numberDecimal) return Number(p.$numberDecimal) || 0;
  return 0;
};

const TYPE_NAMES = {
  category: 'Furniture category',
  provider: 'Provider',
  color: 'Color',
  line: 'Line',
  currency: 'Currency',
  material: 'Material',
  legs: 'Legs',
  origin: 'Origin',
};

export default function ProductCollections() {
  const { user } = useAuth();
  const token = user?.thalosToken || localStorage.getItem('thalosToken');

  const api = useMemo(() => new FurnitureVariantApi(token), [token]);
  const tagTypeApi = useMemo(() => new TagTypeApi(token), [token]);
  const categoriesApi = useMemo(() => new CategoriesApi(token), [token]);

  const toast = useApiToast();

  const [rows, setRows] = useState([]);
  const [rawRows, setRawRows] = useState([]);
  const [open, setOpen] = useState(false);
  const [editRow, setEditRow] = useState(null);
  const [loading, setLoading] = useState(true);

  const [statusFilter, setStatusFilter] = useState('All');

  // Tags
  const [loadingTags, setLoadingTags] = useState(true);
  const [typeMap, setTypeMap] = useState({});
  const [byType, setByType] = useState({
    [TYPE_NAMES.category]: [],
    [TYPE_NAMES.provider]: [],
    [TYPE_NAMES.color]: [],
    [TYPE_NAMES.line]: [],
    [TYPE_NAMES.currency]: [],
    [TYPE_NAMES.material]: [],
    [TYPE_NAMES.legs]: [],
    [TYPE_NAMES.origin]: [],
  });

  const buildLabelResolver = (typeName) => {
    const list = byType[typeName] || [];
    return (value) => {
      if (!value && value !== 0) return '—';
      if (list.some(t => t.tagName === value)) return value; // ya es tagName
      const found = list.find(t =>
        t.id === value ||
        t._id === value ||
        t._id?.$oid === value ||
        t.slug === value
      );
      return found?.tagName || String(value);
    };
  };

  const labelCategory = useMemo(() => buildLabelResolver(TYPE_NAMES.category), [byType]);
  const labelProvider = useMemo(() => buildLabelResolver(TYPE_NAMES.provider), [byType]);
  const labelColor = useMemo(() => buildLabelResolver(TYPE_NAMES.color), [byType]);
  const labelLine = useMemo(() => buildLabelResolver(TYPE_NAMES.line), [byType]);
  const labelCurrency = useMemo(() => buildLabelResolver(TYPE_NAMES.currency), [byType]);
  const labelMaterial = useMemo(() => buildLabelResolver(TYPE_NAMES.material), [byType]);
  const labelLegs = useMemo(() => buildLabelResolver(TYPE_NAMES.legs), [byType]);
  const labelOrigin = useMemo(() => buildLabelResolver(TYPE_NAMES.origin), [byType]);

  // Cargar TagTypes + Tags
  useEffect(() => {
    let mounted = true;
    (async () => {
      try {
        const [types, tags] = await Promise.all([tagTypeApi.getAll(), categoriesApi.getAll()]);
        const tmap = {};
        types?.forEach(t => {
          if (!t?.typeName || !t?._id) return;
          tmap[t.typeName] = t._id;
        });
        const group = (tname) => {
          const tid = tmap[tname];
          if (!tid) return [];
          return (tags || []).filter(tag => tag?.typeId === tid);
        };
        if (mounted) {
          setTypeMap(tmap);
          setByType({
            [TYPE_NAMES.category]: group(TYPE_NAMES.category),
            [TYPE_NAMES.provider]: group(TYPE_NAMES.provider),
            [TYPE_NAMES.color]: group(TYPE_NAMES.color),
            [TYPE_NAMES.line]: group(TYPE_NAMES.line),
            [TYPE_NAMES.currency]: group(TYPE_NAMES.currency),
            [TYPE_NAMES.material]: group(TYPE_NAMES.material),
            [TYPE_NAMES.legs]: group(TYPE_NAMES.legs),
            [TYPE_NAMES.origin]: group(TYPE_NAMES.origin),
          });
        }
      } catch (e) {
        console.error('Failed loading TagTypes/Tags', e);
      } finally {
        if (mounted) setLoadingTags(false);
      }
    })();
    return () => { mounted = false; };
  }, [tagTypeApi, categoriesApi]);

  // Cargar variants
  const load = async () => {
    try {
      setLoading(true);
      const data = await api.getAllVariants();
      const normalized = (data || []).map((r, idx) => ({
        id: r.id || r._id || `row-${idx}`,
        _Id: r._id || r._Id || '',
        modelId: r.modelId ?? '',
        name: r.name ?? '',
        categoryId: r.categoryId ?? '',
        providerId: r.providerId ?? '',
        color: r.color ?? '',
        line: r.line ?? '',
        stock: Number(r.stock ?? 0),
        price: parsePrice(r.price),
        currency: r.currency ?? 'USD',
        attributes: {
          material: r?.attributes?.material ?? '',
          legs: r?.attributes?.legs ?? '',
          origin: r?.attributes?.origin ?? '',
        },
        status: r.status ?? 'Active',
        createdAt: r.createdAt ?? null,
        createdBy: r.createdBy ?? null,
      }));
      setRawRows(normalized);
    } catch (err) {
      console.error(err);
      toast.error(err?.message || 'Error loading variants');
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => { load(); /* eslint-disable-next-line */ }, []);
  useEffect(() => {
    if (statusFilter === 'All') {
      setRows(rawRows);
    } else {
      const want = statusFilter.toLowerCase();
      setRows(rawRows.filter(r => String(r.status ?? 'Active').toLowerCase() === want));
    }
  }, [statusFilter, rawRows]);

  const columns = [
   {
      field: 'actions',
      headerName: '',
      width: 130,
      sortable: false,
      filterable: false,
      disableExport: true,
      renderCell: (params) => (
        <Box display="flex" alignItems="center" justifyContent="flex-start" height="100%" gap={1}>
          <IconButton
            size="small"
            sx={{
              backgroundColor: '#DFCCBC',
              color: '#26201A',
              '&:hover': { backgroundColor: '#C2B2A4' },
              borderRadius: 2,
              p: 1,
            }}
            onClick={() => handleEditClick(params)}
          >
            <EditRoundedIcon fontSize="small" />
          </IconButton>
          <IconButton
            size="small"
            sx={{
              backgroundColor: '#FBE9E7',
              color: '#C62828',
              '&:hover': { backgroundColor: '#EF9A9A' },
              borderRadius: 2,
              p: 1,
            }}
            onClick={() => handleDeleteClick(params?.row)}
          >
            <DeleteRoundedIcon fontSize="small" />
          </IconButton>
        </Box>
      ),
    },

    
    { field: 'name', headerName: 'Name', width: 200 },
    { field: 'categoryId', headerName: 'Category', width: 170, valueGetter: (p) => labelCategory(p?.row?.categoryId) },
    { field: 'providerId', headerName: 'Provider', width: 170, valueGetter: (p) => labelProvider(p?.row?.providerId) },
    { field: 'color', headerName: 'Color', width: 130, valueGetter: (p) => labelColor(p?.row?.color) },
    { field: 'line', headerName: 'Line', width: 130, valueGetter: (p) => labelLine(p?.row?.line) },
    {
      field: 'price',
      headerName: 'Price',
      width: 130,
      type: 'number',
      valueGetter: (p) => parsePrice(p?.row?.price),
      renderCell: (p) => {
        const currency = labelCurrency(p?.row?.currency || 'USD');
        const val = parsePrice(p?.row?.price);
        try {
          return new Intl.NumberFormat(undefined, { style: 'currency', currency: currency || 'USD' }).format(val);
        } catch {
          return `${currency} ${val.toFixed(2)}`;
        }
      }
    },
    { field: 'currency', headerName: 'Currency', width: 120, valueGetter: (p) => labelCurrency(p?.row?.currency) },
    { field: 'stock', headerName: 'Stock', width: 100, type: 'number', valueGetter: (p) => Number(p?.row?.stock ?? 0) },
    { field: 'attributes.material', headerName: 'Material', width: 150, valueGetter: (p) => labelMaterial(p?.row?.attributes?.material) },
    { field: 'attributes.legs', headerName: 'Legs', width: 140, valueGetter: (p) => labelLegs(p?.row?.attributes?.legs) },
    { field: 'attributes.origin', headerName: 'Origin', width: 150, valueGetter: (p) => labelOrigin(p?.row?.attributes?.origin) },
    { field: 'status', headerName: 'Status', width: 120 }
  ];

  return (
    <Box sx={{ height: 'calc(100vh - 64px - 64px)', display: 'flex', flexDirection: 'column', gap: 2 }}>
      <SectionContainer>
      <Box display="flex" alignItems="center" justifyContent="space-between" mb={2} flexWrap="wrap" gap={2}>
        <Typography variant="h6">Furniture Variants</Typography>
        <Box display="flex" alignItems="center" gap={2}>
          <ToggleButtonGroup
            value={statusFilter}
            exclusive
            onChange={(_, v) => v && setStatusFilter(v)}
            size="small"
          >
            <ToggleButton value="Active">Active</ToggleButton>
            <ToggleButton value="All">All</ToggleButton>
            <ToggleButton value="Inactive">Inactive</ToggleButton>
          </ToggleButtonGroup>
          <Button variant="contained" className="button-gold" startIcon={<AddRoundedIcon />} onClick={() => { setEditRow(null); setOpen(true); }}>
            Add Variant
          </Button>
        </Box>
      </Box>

      <Box sx={{ flex: 1, minHeight: 0 }}>
        <DataGrid
          rows={rows}
          columns={columns}
          disableRowSelectionOnClick
          loading={loading || loadingTags}
          pageSizeOptions={[50, 100, 200]}
          initialState={{
            pagination: { paginationModel: { pageSize: 50 } },
            columns: { columnVisibilityModel: { id: false, _Id: false } },
          }}
          getRowHeight={() => 'auto'}
          sx={{
            '& .MuiDataGrid-cell': { display: 'flex', alignItems: 'center' },
            '& .MuiDataGrid-columnHeader': { display: 'flex', alignItems: 'center' },
            '& .MuiDataGrid-virtualScroller': { overflowX: 'hidden' },
          }}
        />
      </Box>

      <Dialog open={open} onClose={() => setOpen(false)} maxWidth="md" fullWidth>
        <DialogTitle>{editRow ? 'Edit Product Collection' : 'Add Product Collection'}</DialogTitle>
        <DialogContent>
          <AddOrEditProductCollectionForm
            initialData={editRow}
            onAdd={(saved) => {
              setOpen(false);
              if (editRow) {
                setRawRows(prev => prev.map(r => (r.id === editRow.id ? { ...saved } : r)));
              } else {
                setRawRows(prev => [{ ...saved, id: saved.id || saved._id || `row-${Date.now()}` }, ...prev]);
              }
            }}
            onCancel={() => setOpen(false)}
          />
        </DialogContent>
      </Dialog>
      </SectionContainer>
    </Box>
  );
}
