import { useState } from "react";
import { useParams } from "react-router-dom";
import { usePullsQuery, useClosePRMutation, useReopenPRMutation, useDeletePRMutation } from "@/hooks/usePullsQuery";
import { useProjectsQuery } from "@/hooks/useProjectsQuery";
import { PageHeader } from "@/components/ui/PageHeader";
import { LoadingState } from "@/components/ui/LoadingState";
import { EmptyState } from "@/components/ui/EmptyState";
import { ErrorState } from "@/components/ui/ErrorState";
import { GitPullRequest, User, Calendar, Lock, Unlock, Trash2 } from "lucide-react";
import type { PullRequestResponse } from "@/client/model";

const STATUS_COLORS: Record<string, string> = {
  open: "var(--status-online)",
  closed: "var(--status-dnd)",
  merged: "var(--accent)",
};

export function PullsPage() {
  const { projectName } = useParams<{ projectName: string }>();
  const { data: pulls = [], isLoading, error, refetch } = usePullsQuery(projectName);
  const { data: projects = [] } = useProjectsQuery();
  const activeProject = projects.find((p) => p.name === projectName);

  const closePR = useClosePRMutation();
  const reopenPR = useReopenPRMutation();
  const deletePR = useDeletePRMutation();

  const [confirmDelete, setConfirmDelete] = useState<number | null>(null);

  const handleDelete = (pr: PullRequestResponse) => {
    if (confirmDelete === pr.number) {
      deletePR.mutate({ projectName: projectName!, repo: pr.repo, prNumber: pr.number });
      setConfirmDelete(null);
    } else {
      setConfirmDelete(pr.number);
    }
  };

  if (!projectName) {
    return (
      <div className="h-full flex items-center justify-center">
        <EmptyState
          icon={<GitPullRequest className="w-6 h-6" />}
          title="No project selected"
          description="Select a project to view its pull requests"
        />
      </div>
    );
  }

  if (isLoading) {
    return (
      <div className="h-full flex items-center justify-center">
        <LoadingState message="Loading pull requests..." />
      </div>
    );
  }

  if (error) {
    return (
      <div className="h-full flex items-center justify-center">
        <ErrorState
          title="Failed to load pull requests"
          message={error instanceof Error ? error.message : "An error occurred"}
          onRetry={() => refetch()}
        />
      </div>
    );
  }

  return (
    <div className="p-6 h-full overflow-auto">
      <PageHeader
        title="Pull Requests"
        description={`${activeProject?.display_name || "Project"} pull requests`}
      />

      {pulls.length === 0 ? (
        <EmptyState
          icon={<GitPullRequest className="w-6 h-6" />}
          title="No pull requests"
          description="Create a pull request to propose changes"
        />
      ) : (
        <div className="space-y-3">
          {pulls.map((pr: PullRequestResponse) => (
            <div
              key={pr.number}
              className="rounded-lg border p-4 transition-colors"
              style={{ backgroundColor: "var(--surface-elevated)", borderColor: "var(--border-default)" }}
            >
              <div className="flex items-start gap-3">
                <GitPullRequest
                  className="w-5 h-5 mt-0.5"
                  style={{ color: STATUS_COLORS[pr.status] || "var(--text-muted)" }}
                />
                <div className="flex-1 min-w-0">
                  <div className="flex items-center gap-2 mb-1">
                    <h3 className="text-base font-semibold line-clamp-1" style={{ color: "var(--text-primary)" }}>
                      {pr.title}
                    </h3>
                    <span
                      className="px-2 py-0.5 rounded-full text-xs font-medium"
                      style={{ backgroundColor: STATUS_COLORS[pr.status] || "var(--text-muted)", color: "var(--text-inverse)" }}
                    >
                      {pr.status}
                    </span>
                  </div>
                  <p className="text-xs mb-2" style={{ color: "var(--text-muted)" }}>
                    #{pr.number} {pr.head || "?"} → {pr.base || "?"}
                  </p>
                  <div className="flex items-center gap-4 text-xs" style={{ color: "var(--text-muted)" }}>
                    {pr.author_username && (
                      <span className="flex items-center gap-1">
                        <User className="w-3 h-3" />
                        {pr.author_username}
                      </span>
                    )}
                    {pr.updated_at && (
                      <span className="flex items-center gap-1">
                        <Calendar className="w-3 h-3" />
                        {new Date(pr.updated_at).toLocaleDateString()}
                      </span>
                    )}
                  </div>
                  {/* Actions */}
                  <div className="flex items-center gap-2 mt-3">
                    {pr.status === "open" ? (
                      <button
                        className="flex items-center gap-1 text-xs px-2 py-1 rounded border hover:bg-red-500/10 hover:border-red-500/50 transition-colors"
                        style={{ borderColor: "var(--border-default)", color: "var(--text-muted)" }}
                        onClick={() => closePR.mutate({ projectName: projectName!, repo: pr.repo, prNumber: pr.number })}
                        disabled={closePR.isPending}
                      >
                        <Lock className="w-3 h-3" /> Close
                      </button>
                    ) : (
                      <button
                        className="flex items-center gap-1 text-xs px-2 py-1 rounded border hover:bg-green-500/10 hover:border-green-500/50 transition-colors"
                        style={{ borderColor: "var(--border-default)", color: "var(--text-muted)" }}
                        onClick={() => reopenPR.mutate({ projectName: projectName!, repo: pr.repo, prNumber: pr.number })}
                        disabled={reopenPR.isPending}
                      >
                        <Unlock className="w-3 h-3" /> Reopen
                      </button>
                    )}
                    <button
                      className="flex items-center gap-1 text-xs px-2 py-1 rounded border hover:bg-destructive/10 hover:border-destructive/50 transition-colors text-destructive"
                      style={{ borderColor: "var(--border-default)" }}
                      onClick={() => handleDelete(pr)}
                      disabled={deletePR.isPending}
                    >
                      <Trash2 className="w-3 h-3" />
                      {confirmDelete === pr.number ? "Confirm" : "Delete"}
                    </button>
                  </div>
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

export default PullsPage;
