"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { ReportListTable } from "@/components/report/ReportListTable";
import { Plus, Search, RotateCcw } from "lucide-react";
import { useReportList } from "@/hooks/useReportList";

export default function ReportManagementPage() {
  const router = useRouter();
  const [searchText, setSearchText] = useState("");

  const { reports, total, page, limit, isLoading, refetch, setPage, handleSearch } = useReportList();

  const handleSearchClick = () => {
    handleSearch(searchText);
  };

  const handleReset = () => {
    setSearchText("");
    handleSearch("");
  };

  const handleCreateNew = () => {
    // 새 리포트는 'new'라는 특수 ID로 디자이너 진입
    router.push("/admin/screenMng/reportList/designer/new");
  };

  return (
    <div className="min-h-screen bg-muted">
      <div className="w-full max-w-none space-y-8 px-4 py-8">
        {/* 페이지 제목 */}
        <div className="flex items-center justify-between rounded-lg border bg-white p-6 shadow-sm">
          <div>
            <h1 className="text-3xl font-bold text-foreground">리포트 관리</h1>
            <p className="mt-2 text-muted-foreground">리포트를 생성하고 관리합니다</p>
          </div>
          <Button onClick={handleCreateNew} className="gap-2">
            <Plus className="h-4 w-4" />새 리포트
          </Button>
        </div>

        {/* 검색 영역 */}
        <Card className="shadow-sm">
          <CardHeader className="bg-muted/50">
            <CardTitle className="flex items-center gap-2">
              <Search className="h-5 w-5" />
              검색
            </CardTitle>
          </CardHeader>
          <CardContent className="pt-6">
            <div className="flex gap-2">
              <Input
                placeholder="리포트명으로 검색..."
                value={searchText}
                onChange={(e) => setSearchText(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === "Enter") {
                    handleSearchClick();
                  }
                }}
                className="flex-1"
              />
              <Button onClick={handleSearchClick} className="gap-2">
                <Search className="h-4 w-4" />
                검색
              </Button>
              <Button onClick={handleReset} variant="outline" className="gap-2">
                <RotateCcw className="h-4 w-4" />
                초기화
              </Button>
            </div>
          </CardContent>
        </Card>

        {/* 리포트 목록 */}
        <Card className="shadow-sm">
          <CardHeader className="bg-muted/50">
            <CardTitle className="flex items-center justify-between">
              <span className="flex items-center gap-2">
                📋 리포트 목록
                <span className="text-muted-foreground text-sm font-normal">(총 {total}건)</span>
              </span>
            </CardTitle>
          </CardHeader>
          <CardContent className="p-0">
            <ReportListTable
              reports={reports}
              total={total}
              page={page}
              limit={limit}
              isLoading={isLoading}
              onPageChange={setPage}
              onRefresh={refetch}
            />
          </CardContent>
        </Card>
      </div>
    </div>
  );
}
