import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid";
import { languageStore, routeStore } from "@shared";
import { useEffect, useState } from "react";
import { observer } from "mobx-react-lite";
import { Eye, Trash2 } from "lucide-react";
import { useNavigate } from "react-router-dom";
import { CreateButton, DeleteModal, LanguageSwitcher } from "@widgets";

export const RouteListPage = observer(() => {
  const { routes, getRoutes, deleteRoute } = routeStore;
  const navigate = useNavigate();
  const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  const [rowId, setRowId] = useState<number | null>(null); // Lifted state
  const { language } = languageStore;

  useEffect(() => {
    getRoutes();
  }, [language]);

  const columns: GridColDef[] = [
    {
      field: "carrier",
      headerName: "Перевозчик",
      width: 250,
    },
    {
      field: "route_number",
      headerName: "Номер маршрута",
      flex: 1,
    },
    {
      field: "route_direction",
      headerName: "Направление",
      flex: 1,
      renderCell: (params: GridRenderCellParams) => {
        return (
          <p
            className={
              params.row.route_direction === "Прямой"
                ? "text-green-500"
                : "text-red-500"
            }
          >
            {params.row.route_direction}
          </p>
        );
      },
    },
    {
      field: "actions",
      headerName: "Действия",
      width: 140,
      align: "center",
      headerAlign: "center",
      renderCell: (params: GridRenderCellParams) => {
        return (
          <div className="flex h-full gap-7 justify-center items-center">
            <button onClick={() => navigate(`/route/${params.row.id}`)}>
              <Eye size={20} className="text-green-500" />
            </button>
            <button
              onClick={() => {
                setIsDeleteModalOpen(true);
                setRowId(params.row.id);
              }}
            >
              <Trash2 size={20} className="text-red-500" />
            </button>
          </div>
        );
      },
    },
  ];

  const rows = routes.map((route) => ({
    id: route.id,
    carrier: route.carrier,
    route_number: route.route_number,
    route_direction: route.route_direction ? "Прямой" : "Обратный",
  }));

  return (
    <>
      <LanguageSwitcher />

      <div style={{ width: "100%" }}>
        <div className="flex justify-between items-center mb-10">
          <h1 className="text-2xl">Маршруты</h1>
          <CreateButton label="Создать маршрут" path="/route/create" />
        </div>
        <DataGrid
          rows={rows}
          columns={columns}
          hideFooterPagination
          hideFooter
        />
      </div>

      <DeleteModal
        open={isDeleteModalOpen}
        onDelete={async () => {
          if (rowId) {
            deleteRoute(rowId);
          }
          setIsDeleteModalOpen(false);
          setRowId(null);
        }}
        onCancel={() => {
          setIsDeleteModalOpen(false);
          setRowId(null);
        }}
      />
    </>
  );
});
