"use client";

import { useState } from "react";
import Image from "next/image";
import { api } from "~/trpc/react";
import type { Script } from "~/types/script";
import { DiffViewer } from "./DiffViewer";
import { TextViewer } from "./TextViewer";
import { ExecutionModeModal } from "./ExecutionModeModal";

interface ScriptDetailModalProps {
  script: Script | null;
  isOpen: boolean;
  onClose: () => void;
  onInstallScript?: (
    scriptPath: string,
    scriptName: string,
    mode?: "local" | "ssh",
    server?: any,
  ) => void;
}

export function ScriptDetailModal({
  script,
  isOpen,
  onClose,
  onInstallScript,
}: ScriptDetailModalProps) {
  const [imageError, setImageError] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [loadMessage, setLoadMessage] = useState<string | null>(null);
  const [diffViewerOpen, setDiffViewerOpen] = useState(false);
  const [selectedDiffFile, setSelectedDiffFile] = useState<string | null>(null);
  const [textViewerOpen, setTextViewerOpen] = useState(false);
  const [executionModeOpen, setExecutionModeOpen] = useState(false);

  // Check if script files exist locally
  const {
    data: scriptFilesData,
    refetch: refetchScriptFiles,
    isLoading: scriptFilesLoading,
  } = api.scripts.checkScriptFiles.useQuery(
    { slug: script?.slug ?? "" },
    { enabled: !!script && isOpen },
  );

  // Compare local and remote script content (run in parallel, not dependent on scriptFilesData)
  const {
    data: comparisonData,
    refetch: refetchComparison,
    isLoading: comparisonLoading,
  } = api.scripts.compareScriptContent.useQuery(
    { slug: script?.slug ?? "" },
    { enabled: !!script && isOpen },
  );

  // Load script mutation
  const loadScriptMutation = api.scripts.loadScript.useMutation({
    onSuccess: (data) => {
      setIsLoading(false);
      if (data.success) {
        const message =
          "message" in data ? data.message : "Script loaded successfully";
        setLoadMessage(`✅ ${message}`);
        // Refetch script files status and comparison data to update the UI
        void refetchScriptFiles();
        void refetchComparison();
      } else {
        const error = "error" in data ? data.error : "Failed to load script";
        setLoadMessage(`❌ ${error}`);
      }
      // Clear message after 5 seconds
      setTimeout(() => setLoadMessage(null), 5000);
    },
    onError: (error) => {
      setIsLoading(false);
      setLoadMessage(`❌ Error: ${error.message}`);
      setTimeout(() => setLoadMessage(null), 5000);
    },
  });

  if (!isOpen || !script) return null;

  const handleImageError = () => {
    setImageError(true);
  };

  const handleBackdropClick = (e: React.MouseEvent) => {
    if (e.target === e.currentTarget) {
      onClose();
    }
  };

  const handleLoadScript = async () => {
    if (!script) return;

    setIsLoading(true);
    setLoadMessage(null);
    loadScriptMutation.mutate({ slug: script.slug });
  };

  const handleInstallScript = () => {
    if (!script) return;
    setExecutionModeOpen(true);
  };

  const handleExecuteScript = (mode: "local" | "ssh", server?: any) => {
    if (!script || !onInstallScript) return;

    // Find the script path (CT or tools)
    const scriptMethod = script.install_methods?.find(
      (method) => method.script,
    );
    if (scriptMethod?.script) {
      const scriptPath = `scripts/${scriptMethod.script}`;
      const scriptName = script.name;

      // Pass execution mode and server info to the parent
      onInstallScript(scriptPath, scriptName, mode, server);

      // Scroll to top of the page to see the terminal
      window.scrollTo({ top: 0, behavior: "smooth" });

      onClose(); // Close the modal when starting installation
    }
  };

  const handleViewScript = () => {
    setTextViewerOpen(true);
  };

  return (
    <div
      className="bg-opacity-50 fixed inset-0 z-50 flex items-center justify-center bg-black p-4"
      onClick={handleBackdropClick}
    >
      <div className="max-h-[90vh] w-full max-w-4xl overflow-y-auto rounded-lg bg-white shadow-xl dark:bg-gray-800">
        {/* Header */}
        <div className="flex items-center justify-between border-b border-gray-200 p-6 dark:border-gray-700">
          <div className="flex items-center space-x-4">
            {script.logo && !imageError ? (
              <Image
                src={script.logo}
                alt={`${script.name} logo`}
                width={64}
                height={64}
                className="h-16 w-16 rounded-lg object-contain"
                onError={handleImageError}
              />
            ) : (
              <div className="flex h-16 w-16 items-center justify-center rounded-lg bg-gray-200 dark:bg-gray-700">
                <span className="text-2xl font-semibold text-gray-500 dark:text-gray-400">
                  {script.name.charAt(0).toUpperCase()}
                </span>
              </div>
            )}
            <div>
              <h2 className="text-2xl font-bold text-gray-900 dark:text-gray-100">
                {script.name}
              </h2>
              <div className="mt-1 flex items-center space-x-2">
                <span
                  className={`inline-flex items-center rounded-full px-3 py-1 text-sm font-medium ${
                    script.type === "ct"
                      ? "bg-blue-100 text-blue-800"
                      : "bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200"
                  }`}
                >
                  {script.type.toUpperCase()}
                </span>
                {script.updateable && (
                  <span className="inline-flex items-center rounded-full bg-green-100 px-3 py-1 text-sm font-medium text-green-800">
                    Updateable
                  </span>
                )}
                {script.privileged && (
                  <span className="inline-flex items-center rounded-full bg-red-100 px-3 py-1 text-sm font-medium text-red-800">
                    Privileged
                  </span>
                )}
              </div>
            </div>
          </div>
          <div className="flex items-center space-x-4">
            {/* Install Button - only show if script files exist */}
            {scriptFilesData?.success &&
              scriptFilesData.ctExists &&
              onInstallScript && (
                <button
                  onClick={handleInstallScript}
                  className="flex items-center space-x-2 rounded-lg bg-blue-600 px-4 py-2 font-medium text-white transition-colors hover:bg-blue-700"
                >
                  <svg
                    className="h-4 w-4"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"
                    />
                  </svg>
                  <span>Install</span>
                </button>
              )}

            {/* View Button - only show if script files exist */}
            {scriptFilesData?.success &&
              (scriptFilesData.ctExists || scriptFilesData.installExists) && (
                <button
                  onClick={handleViewScript}
                  className="flex items-center space-x-2 rounded-lg bg-purple-600 px-4 py-2 font-medium text-white transition-colors hover:bg-purple-700"
                >
                  <svg
                    className="h-4 w-4"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
                    />
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
                    />
                  </svg>
                  <span>View</span>
                </button>
              )}

            {/* Load/Update Script Button */}
            {(() => {
              const hasLocalFiles =
                scriptFilesData?.success &&
                (scriptFilesData.ctExists || scriptFilesData.installExists);
              const hasDifferences =
                comparisonData?.success && comparisonData.hasDifferences;
              const isUpToDate = hasLocalFiles && !hasDifferences;

              if (!hasLocalFiles) {
                // No local files - show Load Script button
                return (
                  <button
                    onClick={handleLoadScript}
                    disabled={isLoading}
                    className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
                      isLoading
                        ? "cursor-not-allowed bg-gray-400 text-white"
                        : "bg-green-600 text-white hover:bg-green-700"
                    }`}
                  >
                    {isLoading ? (
                      <>
                        <div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
                        <span>Loading...</span>
                      </>
                    ) : (
                      <>
                        <svg
                          className="h-4 w-4"
                          fill="none"
                          stroke="currentColor"
                          viewBox="0 0 24 24"
                        >
                          <path
                            strokeLinecap="round"
                            strokeLinejoin="round"
                            strokeWidth={2}
                            d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
                          />
                        </svg>
                        <span>Load Script</span>
                      </>
                    )}
                  </button>
                );
              } else if (isUpToDate) {
                // Local files exist and are up to date - show disabled Update button
                return (
                  <button
                    disabled
                    className="flex cursor-not-allowed items-center space-x-2 rounded-lg bg-gray-400 px-4 py-2 font-medium text-white transition-colors"
                  >
                    <svg
                      className="h-4 w-4"
                      fill="none"
                      stroke="currentColor"
                      viewBox="0 0 24 24"
                    >
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth={2}
                        d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
                      />
                    </svg>
                    <span>Up to Date</span>
                  </button>
                );
              } else {
                // Local files exist but have differences - show Update button
                return (
                  <button
                    onClick={handleLoadScript}
                    disabled={isLoading}
                    className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
                      isLoading
                        ? "cursor-not-allowed bg-gray-400 text-white"
                        : "bg-orange-600 text-white hover:bg-orange-700"
                    }`}
                  >
                    {isLoading ? (
                      <>
                        <div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
                        <span>Updating...</span>
                      </>
                    ) : (
                      <>
                        <svg
                          className="h-4 w-4"
                          fill="none"
                          stroke="currentColor"
                          viewBox="0 0 24 24"
                        >
                          <path
                            strokeLinecap="round"
                            strokeLinejoin="round"
                            strokeWidth={2}
                            d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
                          />
                        </svg>
                        <span>Update Script</span>
                      </>
                    )}
                  </button>
                );
              }
            })()}
            <button
              onClick={onClose}
              className="text-gray-400 transition-colors hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300"
            >
              <svg
                className="h-6 w-6"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M6 18L18 6M6 6l12 12"
                />
              </svg>
            </button>
          </div>
        </div>

        {/* Load Message */}
        {loadMessage && (
          <div className="mx-6 mb-4 rounded-lg bg-blue-50 p-3 text-sm text-blue-800">
            {loadMessage}
          </div>
        )}

        {/* Script Files Status */}
        {(scriptFilesLoading || comparisonLoading) && (
          <div className="mx-6 mb-4 rounded-lg bg-blue-50 p-3 text-sm">
            <div className="flex items-center space-x-2">
              <div className="h-4 w-4 animate-spin rounded-full border-b-2 border-blue-600"></div>
              <span>Loading script status...</span>
            </div>
          </div>
        )}

        {scriptFilesData?.success &&
          !scriptFilesLoading &&
          (() => {
            // Determine script type from the first install method
            const firstScript = script?.install_methods?.[0]?.script;
            let scriptType = "Script";
            if (firstScript?.startsWith("ct/")) {
              scriptType = "CT Script";
            } else if (firstScript?.startsWith("tools/")) {
              scriptType = "Tools Script";
            } else if (firstScript?.startsWith("vm/")) {
              scriptType = "VM Script";
            } else if (firstScript?.startsWith("vw/")) {
              scriptType = "VW Script";
            }

            return (
              <div className="mx-6 mb-4 rounded-lg bg-gray-50 p-3 text-sm text-gray-700 dark:bg-gray-700 dark:text-gray-300">
                <div className="flex items-center space-x-4">
                  <div className="flex items-center space-x-2">
                    <div
                      className={`h-2 w-2 rounded-full ${scriptFilesData.ctExists ? "bg-green-500" : "bg-gray-300"}`}
                    ></div>
                    <span>
                      {scriptType}:{" "}
                      {scriptFilesData.ctExists ? "Available" : "Not loaded"}
                    </span>
                  </div>
                  <div className="flex items-center space-x-2">
                    <div
                      className={`h-2 w-2 rounded-full ${scriptFilesData.installExists ? "bg-green-500" : "bg-gray-300"}`}
                    ></div>
                    <span>
                      Install Script:{" "}
                      {scriptFilesData.installExists
                        ? "Available"
                        : "Not loaded"}
                    </span>
                  </div>
                  {scriptFilesData?.success &&
                    (scriptFilesData.ctExists ||
                      scriptFilesData.installExists) &&
                    comparisonData?.success &&
                    !comparisonLoading && (
                      <div className="flex items-center space-x-2">
                        <div
                          className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-orange-500" : "bg-green-500"}`}
                        ></div>
                        <span>
                          Status:{" "}
                          {comparisonData.hasDifferences
                            ? "Update available"
                            : "Up to date"}
                        </span>
                      </div>
                    )}
                </div>
                {scriptFilesData.files.length > 0 && (
                  <div className="mt-2 text-xs text-gray-600 dark:text-gray-400">
                    Files: {scriptFilesData.files.join(", ")}
                  </div>
                )}
              </div>
            );
          })()}

        {/* Content */}
        <div className="space-y-6 p-6">
          {/* Description */}
          <div>
            <h3 className="mb-2 text-lg font-semibold text-gray-900 dark:text-gray-100">
              Description
            </h3>
            <p className="text-gray-600 dark:text-gray-300">
              {script.description}
            </p>
          </div>

          {/* Basic Information */}
          <div className="grid grid-cols-1 gap-6 md:grid-cols-2">
            <div>
              <h3 className="mb-3 text-lg font-semibold text-gray-900 dark:text-gray-100">
                Basic Information
              </h3>
              <dl className="space-y-2">
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Slug
                  </dt>
                  <dd className="font-mono text-sm text-gray-900 dark:text-gray-100">
                    {script.slug}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Date Created
                  </dt>
                  <dd className="text-sm text-gray-900 dark:text-gray-100">
                    {script.date_created}
                  </dd>
                </div>
                <div>
                  <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                    Categories
                  </dt>
                  <dd className="text-sm text-gray-900 dark:text-gray-100">
                    {script.categories.join(", ")}
                  </dd>
                </div>
                {script.interface_port && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                      Interface Port
                    </dt>
                    <dd className="text-sm text-gray-900 dark:text-gray-100">
                      {script.interface_port}
                    </dd>
                  </div>
                )}
                {script.config_path && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                      Config Path
                    </dt>
                    <dd className="font-mono text-sm text-gray-900 dark:text-gray-100">
                      {script.config_path}
                    </dd>
                  </div>
                )}
              </dl>
            </div>

            <div>
              <h3 className="mb-3 text-lg font-semibold text-gray-900 dark:text-gray-100">
                Links
              </h3>
              <dl className="space-y-2">
                {script.website && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                      Website
                    </dt>
                    <dd className="text-sm">
                      <a
                        href={script.website}
                        target="_blank"
                        rel="noopener noreferrer"
                        className="break-all text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-300"
                      >
                        {script.website}
                      </a>
                    </dd>
                  </div>
                )}
                {script.documentation && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500 dark:text-gray-400">
                      Documentation
                    </dt>
                    <dd className="text-sm">
                      <a
                        href={script.documentation}
                        target="_blank"
                        rel="noopener noreferrer"
                        className="break-all text-blue-600 hover:text-blue-800 dark:text-blue-400 dark:hover:text-blue-300"
                      >
                        {script.documentation}
                      </a>
                    </dd>
                  </div>
                )}
              </dl>
            </div>
          </div>

          {/* Install Methods - Hide for PVE and ADDON types as they typically don't have install methods */}
          {script.install_methods.length > 0 &&
            script.type !== "pve" &&
            script.type !== "addon" && (
              <div>
                <h3 className="mb-3 text-lg font-semibold text-gray-900 dark:text-gray-100">
                  Install Methods
                </h3>
                <div className="space-y-4">
                  {script.install_methods.map((method, index) => (
                    <div
                      key={index}
                      className="rounded-lg border border-gray-200 bg-white p-4 dark:border-gray-600 dark:bg-gray-700"
                    >
                      <div className="mb-3 flex items-center justify-between">
                        <h4 className="font-medium text-gray-900 capitalize dark:text-gray-100">
                          {method.type}
                        </h4>
                        <span className="font-mono text-sm text-gray-500 dark:text-gray-400">
                          {method.script}
                        </span>
                      </div>
                      <div className="grid grid-cols-2 gap-4 text-sm md:grid-cols-4">
                        <div>
                          <dt className="font-medium text-gray-500 dark:text-gray-400">
                            CPU
                          </dt>
                          <dd className="text-gray-900 dark:text-gray-100">
                            {method.resources.cpu} cores
                          </dd>
                        </div>
                        <div>
                          <dt className="font-medium text-gray-500 dark:text-gray-400">
                            RAM
                          </dt>
                          <dd className="text-gray-900 dark:text-gray-100">
                            {method.resources.ram} MB
                          </dd>
                        </div>
                        <div>
                          <dt className="font-medium text-gray-500 dark:text-gray-400">
                            HDD
                          </dt>
                          <dd className="text-gray-900 dark:text-gray-100">
                            {method.resources.hdd} GB
                          </dd>
                        </div>
                        <div>
                          <dt className="font-medium text-gray-500 dark:text-gray-400">
                            OS
                          </dt>
                          <dd className="text-gray-900 dark:text-gray-100">
                            {method.resources.os} {method.resources.version}
                          </dd>
                        </div>
                      </div>
                    </div>
                  ))}
                </div>
              </div>
            )}

          {/* Default Credentials */}
          {(script.default_credentials.username ??
            script.default_credentials.password) && (
            <div>
              <h3 className="mb-3 text-lg font-semibold text-gray-900">
                Default Credentials
              </h3>
              <dl className="space-y-2">
                {script.default_credentials.username && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500">
                      Username
                    </dt>
                    <dd className="font-mono text-sm text-gray-900">
                      {script.default_credentials.username}
                    </dd>
                  </div>
                )}
                {script.default_credentials.password && (
                  <div>
                    <dt className="text-sm font-medium text-gray-500">
                      Password
                    </dt>
                    <dd className="font-mono text-sm text-gray-900">
                      {script.default_credentials.password}
                    </dd>
                  </div>
                )}
              </dl>
            </div>
          )}

          {/* Notes */}
          {script.notes.length > 0 && (
            <div>
              <h3 className="mb-3 text-lg font-semibold text-gray-900 dark:text-gray-100">
                Notes
              </h3>
              <ul className="space-y-2">
                {script.notes.map((note, index) => {
                  // Handle both object and string note formats
                  const noteText = typeof note === "string" ? note : note.text;
                  const noteType =
                    typeof note === "string" ? "info" : note.type;

                  return (
                    <li
                      key={index}
                      className={`rounded-lg p-3 text-sm ${
                        noteType === "warning"
                          ? "border-l-4 border-yellow-400 bg-yellow-50 text-yellow-800 dark:bg-yellow-900/20 dark:text-yellow-200"
                          : noteType === "error"
                            ? "border-l-4 border-red-400 bg-red-50 text-red-800 dark:bg-red-900/20 dark:text-red-200"
                            : "bg-gray-50 text-gray-600 dark:bg-gray-700 dark:text-gray-300"
                      }`}
                    >
                      <div className="flex items-start">
                        <span
                          className={`mr-2 inline-flex items-center rounded-full px-2 py-1 text-xs font-medium ${
                            noteType === "warning"
                              ? "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200"
                              : noteType === "error"
                                ? "bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200"
                                : "bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200"
                          }`}
                        >
                          {noteType}
                        </span>
                        <span>{noteText}</span>
                      </div>
                    </li>
                  );
                })}
              </ul>
            </div>
          )}
        </div>
      </div>

      {/* Diff Viewer Modal */}
      {selectedDiffFile && (
        <DiffViewer
          scriptSlug={script.slug}
          filePath={selectedDiffFile}
          isOpen={diffViewerOpen}
          onClose={() => {
            setDiffViewerOpen(false);
            setSelectedDiffFile(null);
          }}
        />
      )}

      {/* Text Viewer Modal */}
      {script && (
        <TextViewer
          scriptName={
            script.install_methods
              ?.find((method) => method.script?.startsWith("ct/"))
              ?.script?.split("/")
              .pop() ?? `${script.slug}.sh`
          }
          isOpen={textViewerOpen}
          onClose={() => setTextViewerOpen(false)}
        />
      )}

      {/* Execution Mode Modal */}
      {script && (
        <ExecutionModeModal
          scriptName={script.name}
          isOpen={executionModeOpen}
          onClose={() => setExecutionModeOpen(false)}
          onExecute={handleExecuteScript}
        />
      )}
    </div>
  );
}
