import { useState } from "react";
import { useAlert } from "../context/AlertContext";
import { useAuth } from "../context/AuthContext";
import { Link } from "react-router-dom";
import Forbidden from "../components/Forbidden";
import { Skeleton } from "boneyard-js/react";
import OrdersFixture from "../fixtures/OrdersFixture";
import { motion } from "framer-motion";
import ConfirmModal from "../components/ConfirmModal";

import apiFetch from "../utils/api";
import { formatCurrency, formatDate, czechPlural } from "../utils/formatters";
import SortIcon from "../components/SortIcon";
import useTableSort from "../hooks/useTableSort";
import { useQueryClient } from "@tanstack/react-query";
import { usePaginatedQuery } from "../hooks/usePaginatedQuery";
import { orderListOptions } from "../lib/queries/orders";
import Pagination from "../components/Pagination";

const API_BASE = "/api/admin";

const STATUS_LABELS: Record<string, string> = {
  prijata: "Přijatá",
  v_realizaci: "V realizaci",
  dokoncena: "Dokončená",
  stornovana: "Stornována",
};

const STATUS_CLASSES: Record<string, string> = {
  prijata: "admin-badge-order-prijata",
  v_realizaci: "admin-badge-order-realizace",
  dokoncena: "admin-badge-order-dokoncena",
  stornovana: "admin-badge-order-stornovana",
};

interface Order {
  id: number;
  order_number: string;
  quotation_id: number;
  quotation_number: string;
  customer_name: string;
  status: string;
  created_at: string;
  total: number;
  currency: string;
  invoice_id?: number;
}

export default function Orders() {
  const alert = useAlert();
  const { hasPermission } = useAuth();

  const { sort, order, handleSort, activeSort } = useTableSort("order_number");
  const [search, setSearch] = useState("");
  const [page, setPage] = useState(1);

  const [deleteConfirm, setDeleteConfirm] = useState<{
    show: boolean;
    order: Order | null;
  }>({ show: false, order: null });
  const [deleting, setDeleting] = useState(false);
  const [deleteFiles, setDeleteFiles] = useState(false);

  const queryClient = useQueryClient();
  const {
    items: orders,
    pagination,
    isPending,
    isFetching,
  } = usePaginatedQuery<Order>(orderListOptions({ search, sort, order, page }));

  if (!hasPermission("orders.view")) return <Forbidden />;

  const handleDelete = async () => {
    if (!deleteConfirm.order) return;
    setDeleting(true);
    try {
      const response = await apiFetch(
        `${API_BASE}/orders/${deleteConfirm.order.id}`,
        {
          method: "DELETE",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ delete_files: deleteFiles }),
        },
      );
      const result = await response.json();
      if (result.success) {
        setDeleteConfirm({ show: false, order: null });
        setDeleteFiles(false);
        alert.success(result.message || "Objednávka byla smazána");
        queryClient.invalidateQueries({ queryKey: ["orders"] });
        queryClient.invalidateQueries({ queryKey: ["offers"] });
        queryClient.invalidateQueries({ queryKey: ["projects"] });
        queryClient.invalidateQueries({ queryKey: ["invoices"] });
      } else {
        alert.error(result.error || "Nepodařilo se smazat objednávku");
      }
    } catch {
      alert.error("Chyba připojení");
    } finally {
      setDeleting(false);
    }
  };

  return (
    <Skeleton name="orders" loading={isPending} fixture={<OrdersFixture />}>
      <div>
        <motion.div
          className="admin-page-header"
          initial={{ opacity: 0, y: 12 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.25 }}
        >
          <div>
            <h1 className="admin-page-title">Objednávky</h1>
            <p className="admin-page-subtitle">
              {pagination?.total ?? orders.length}{" "}
              {czechPlural(
                pagination?.total ?? orders.length,
                "objednávka",
                "objednávky",
                "objednávek",
              )}
            </p>
          </div>
        </motion.div>

        <motion.div
          className="admin-card"
          initial={{ opacity: 0, y: 12 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.25, delay: 0.06 }}
          style={{ opacity: isFetching ? 0.6 : 1, transition: "opacity 0.2s" }}
        >
          <div className="admin-card-body">
            <div className="admin-search-bar mb-4">
              <input
                type="text"
                value={search}
                onChange={(e) => {
                  setSearch(e.target.value);
                  setPage(1);
                }}
                className="admin-form-input"
                placeholder="Hledat podle čísla, nabídky, projektu nebo zákazníka..."
              />
            </div>

            {orders.length === 0 ? (
              <div className="admin-empty-state">
                <div className="admin-empty-icon">
                  <svg
                    width="28"
                    height="28"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    strokeWidth="1.5"
                    strokeLinecap="round"
                    strokeLinejoin="round"
                  >
                    <path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z" />
                    <line x1="3" y1="6" x2="21" y2="6" />
                    <path d="M16 10a4 4 0 0 1-8 0" />
                  </svg>
                </div>
                <p>Zatím nejsou žádné objednávky.</p>
                <p className="text-tertiary" style={{ fontSize: "0.875rem" }}>
                  Objednávky se vytvářejí z nabídek.
                </p>
              </div>
            ) : (
              <div className="admin-table-responsive">
                <table className="admin-table">
                  <thead>
                    <tr>
                      <th
                        style={{ cursor: "pointer" }}
                        onClick={() => handleSort("order_number")}
                      >
                        Číslo{" "}
                        <SortIcon
                          column="order_number"
                          sort={activeSort}
                          order={order}
                        />
                      </th>
                      <th>Nabídka</th>
                      <th>Zákazník</th>
                      <th
                        style={{ cursor: "pointer" }}
                        onClick={() => handleSort("status")}
                      >
                        Stav{" "}
                        <SortIcon
                          column="status"
                          sort={activeSort}
                          order={order}
                        />
                      </th>
                      <th
                        style={{ cursor: "pointer" }}
                        onClick={() => handleSort("created_at")}
                      >
                        Datum{" "}
                        <SortIcon
                          column="created_at"
                          sort={activeSort}
                          order={order}
                        />
                      </th>
                      <th className="text-right">Celkem</th>
                      <th>Akce</th>
                    </tr>
                  </thead>
                  <tbody>
                    {(orders as Order[]).map((o) => (
                      <tr key={o.id}>
                        <td className="admin-mono">
                          <Link to={`/orders/${o.id}`} className="link-accent">
                            {o.order_number}
                          </Link>
                        </td>
                        <td>
                          <Link
                            to={`/offers/${o.quotation_id}`}
                            className="text-secondary"
                            style={{ textDecoration: "none" }}
                          >
                            {o.quotation_number}
                          </Link>
                        </td>
                        <td>{o.customer_name || "—"}</td>
                        <td>
                          <span
                            className={`admin-badge ${STATUS_CLASSES[o.status] || ""}`}
                          >
                            {STATUS_LABELS[o.status] || o.status}
                          </span>
                        </td>
                        <td className="admin-mono">
                          {formatDate(o.created_at)}
                        </td>
                        <td className="admin-mono text-right fw-500">
                          {formatCurrency(o.total, o.currency)}
                        </td>
                        <td>
                          <div className="admin-table-actions">
                            <Link
                              to={`/orders/${o.id}`}
                              className="admin-btn-icon"
                              title="Detail"
                              aria-label="Detail"
                            >
                              <svg
                                width="18"
                                height="18"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                strokeWidth="2"
                              >
                                <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
                                <circle cx="12" cy="12" r="3" />
                              </svg>
                            </Link>
                            {o.invoice_id ? (
                              <Link
                                to={`/invoices/${o.invoice_id}`}
                                className="admin-btn-icon accent"
                                title="Zobrazit fakturu"
                                aria-label="Zobrazit fakturu"
                              >
                                <svg
                                  width="18"
                                  height="18"
                                  viewBox="0 0 24 24"
                                  fill="none"
                                  stroke="currentColor"
                                  strokeWidth="2"
                                >
                                  <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
                                  <polyline points="14 2 14 8 20 8" />
                                  <text
                                    x="12"
                                    y="16.5"
                                    textAnchor="middle"
                                    fill="currentColor"
                                    stroke="none"
                                    fontSize="9"
                                    fontWeight="700"
                                  >
                                    F
                                  </text>
                                </svg>
                              </Link>
                            ) : (
                              hasPermission("invoices.create") && (
                                <Link
                                  to={`/invoices/new?fromOrder=${o.id}`}
                                  className="admin-btn-icon"
                                  title="Vytvořit fakturu"
                                  aria-label="Vytvořit fakturu"
                                >
                                  <svg
                                    width="18"
                                    height="18"
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    strokeWidth="2"
                                  >
                                    <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
                                    <polyline points="14 2 14 8 20 8" />
                                    <line x1="12" y1="11" x2="12" y2="17" />
                                    <line x1="9" y1="14" x2="15" y2="14" />
                                  </svg>
                                </Link>
                              )
                            )}
                            {hasPermission("orders.delete") && (
                              <button
                                onClick={() =>
                                  setDeleteConfirm({ show: true, order: o })
                                }
                                className="admin-btn-icon danger"
                                title="Smazat"
                              >
                                <svg
                                  width="18"
                                  height="18"
                                  viewBox="0 0 24 24"
                                  fill="none"
                                  stroke="currentColor"
                                  strokeWidth="2"
                                >
                                  <polyline points="3 6 5 6 21 6" />
                                  <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
                                </svg>
                              </button>
                            )}
                          </div>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            )}
            <Pagination pagination={pagination} onPageChange={setPage} />
          </div>
        </motion.div>

        <ConfirmModal
          isOpen={deleteConfirm.show}
          onClose={() => {
            setDeleteConfirm({ show: false, order: null });
            setDeleteFiles(false);
          }}
          onConfirm={handleDelete}
          title="Smazat objednávku"
          message={
            <>
              Opravdu chcete smazat objednávku &quot;
              {deleteConfirm.order?.order_number}&quot;? Bude smazán i
              přidružený projekt. Tato akce je nevratná.
              <label
                className="admin-form-checkbox"
                style={{ marginTop: "1rem", display: "flex" }}
              >
                <input
                  type="checkbox"
                  checked={deleteFiles}
                  onChange={(e) => setDeleteFiles(e.target.checked)}
                />
                <span>Smazat i soubory projektu na disku</span>
              </label>
            </>
          }
          confirmText="Smazat"
          cancelText="Zrušit"
          type="danger"
          loading={deleting}
        />
      </div>
    </Skeleton>
  );
}
