"use client";

import Cookies from "js-cookie";
import Link from "next/link";
import { useEffect, useState } from "react";
import { Article } from "@/types/globals";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";

import { Button } from "@/components/ui/button";
import Image from "next/image";
import "react-datepicker/dist/react-datepicker.css";
import { motion } from "framer-motion";
import {
  DashboardCommentIcon,
  DashboardConnectIcon,
  DashboardShareIcon,
  DashboardSpeecIcon,
  DashboardUserIcon,
} from "@/components/icons/dashboard-icon";
import { Checkbox } from "@/components/ui/checkbox";
import CustomPagination from "@/components/layout/custom-pagination";
import { convertDateFormat } from "@/components/utils/global";

type ArticleData = Article & {
  no: number;
  createdAt: string;
};

interface TopPages {
  id: number;
  no: number;
  title: string;
  viewCount: number;
}

interface PostCount {
  userLevelId: number;
  no: number;
  userLevelName: string;
  totalArticle: number;
}

export default function DashboardContainer() {
  const username = Cookies.get("state");
  const fullname = Cookies.get("ufne");
  const [page, setPage] = useState(1);
  const [totalPage, setTotalPage] = useState(1);
  const [topPagesTotalPage, setTopPagesTotalPage] = useState(1);
  const [article, setArticle] = useState<ArticleData[]>([]);
  // const [analyticsView, setAnalyticView] = useState<string[]>(["comment", "view", "share"]);
  // const [startDateValue, setStartDateValue] = useState(parseDate(convertDateFormatNoTimeV2(new Date())));
  // const [postContentDate, setPostContentDate] = useState({
  //   startDate: parseDate(convertDateFormatNoTimeV2(new Date(new Date().setDate(new Date().getDate() - 7)))),
  //   endDate: parseDate(convertDateFormatNoTimeV2(new Date())),
  // });

  const [startDateValue, setStartDateValue] = useState(new Date());
  const [analyticsView, setAnalyticView] = useState<string[]>([]);
  const options = [
    { label: "Comment", value: "comment" },
    { label: "View", value: "view" },
    { label: "Share", value: "share" },
  ];
  const handleChange = (value: string, checked: boolean) => {
    if (checked) {
      setAnalyticView([...analyticsView, value]);
    } else {
      setAnalyticView(analyticsView.filter((v) => v !== value));
    }
  };
  const [postContentDate, setPostContentDate] = useState({
    startDate: new Date(new Date().setDate(new Date().getDate() - 7)),
    endDate: new Date(),
  });

  const [typeDate, setTypeDate] = useState("monthly");
  const [summary, setSummary] = useState<any>();

  const [topPages, setTopPages] = useState<TopPages[]>([]);
  const [postCount, setPostCount] = useState<PostCount[]>([]);

  const getTableNumber = (limit: number, data: any) => {
    if (data) {
      const startIndex = limit * (page - 1);
      let iterate = 0;
      const newData = data.map((value: any) => {
        iterate++;
        value.no = startIndex + iterate;
        return value;
      });
      return newData;
    }
  };

  const getMonthYear = (date: any) => {
    return date.month + " " + date.year;
  };
  const getMonthYearName = (date: any) => {
    const newDate = new Date(date);

    const months = [
      "Januari",
      "Februari",
      "Maret",
      "April",
      "Mei",
      "Juni",
      "Juli",
      "Agustus",
      "September",
      "Oktober",
      "November",
      "Desember",
    ];
    const year = newDate.getFullYear();

    const month = months[newDate.getMonth()];
    return month + " " + year;
  };

  return (
    <div className="space-y-8">
      {/* Stats Cards */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-6 gap-4 p-2">
        {/* User Profile Card */}
        <motion.div
          className="col-span-1 md:col-span-2 bg-white rounded-2xl shadow-md border border-gray-200 p-4 hover:shadow-lg transition-all duration-300"
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.1 }}
        >
          <div className="flex justify-between items-start">
            <div className="space-y-2">
              <p className="text-gray-600 dark:text-black font-medium">Welcome back,</p>
              <p className="text-xl font-semibold text-gray-900 dark:text-black ">{username}</p>
              <p className="text-sm text-gray-500 dark:text-black ">Admin Dashboard</p>
            </div>
            <div className="p-3 bg-gradient-to-br from-blue-50 to-purple-50 rounded-xl dark:text-black ">
              <DashboardUserIcon size={60} />
            </div>
          </div>
        </motion.div>

        {/* Total Posts */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4 hover:shadow-lg transition-all duration-300"
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.2 }}
        >
          <div className="flex flex-col items-center text-center space-y-3">
            <div className="p-3 bg-gradient-to-br from-green-50 to-emerald-50 rounded-xl dark:text-black">
              <DashboardSpeecIcon />
            </div>
            <div>
              <p className="text-3xl font-bold text-gray-900">
                {summary?.totalAll || 0}
              </p>
              <p className="text-sm text-gray-500">Total Posts</p>
            </div>
          </div>
        </motion.div>

        {/* Total Views */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4 hover:shadow-lg transition-all duration-300"
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.3 }}
        >
          <div className="flex flex-col items-center text-center space-y-3">
            <div className="p-3 bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl dark:text-black">
              <DashboardConnectIcon />
            </div>
            <div>
              <p className="text-3xl font-bold text-gray-900">
                {summary?.totalViews || 0}
              </p>
              <p className="text-sm text-gray-500">Total Views</p>
            </div>
          </div>
        </motion.div>

        {/* Total Shares */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4 hover:shadow-lg transition-all duration-300"
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.4 }}
        >
          <div className="flex flex-col items-center text-center space-y-3">
            <div className="p-3 bg-gradient-to-br from-purple-50 to-pink-50 rounded-xl dark:text-black">
              <DashboardShareIcon />
            </div>
            <div>
              <p className="text-3xl font-bold text-gray-900">
                {summary?.totalShares || 0}
              </p>
              <p className="text-sm text-gray-500">Total Shares</p>
            </div>
          </div>
        </motion.div>

        {/* Total Comments */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4 hover:shadow-lg transition-all duration-300"
          initial={{ opacity: 0, y: 20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ delay: 0.5 }}
        >
          <div className="flex flex-col items-center text-center space-y-3">
            <div className="p-3 bg-gradient-to-br from-orange-50 to-red-50 rounded-xl dark:text-black">
              <DashboardCommentIcon size={40} />
            </div>
            <div>
              <p className="text-3xl font-bold text-gray-900">
                {summary?.totalComments || 0}
              </p>
              <p className="text-sm text-gray-500">Total Comments</p>
            </div>
          </div>
        </motion.div>
      </div>

      {/* Content Section */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-4 p-2">
        {/* Analytics Chart */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4"
          initial={{ opacity: 0, x: -20 }}
          animate={{ opacity: 1, x: 0 }}
          transition={{ delay: 0.6 }}
        >
          <div className="flex justify-between items-center mb-6">
            <h3 className="text-lg font-semibold text-gray-900">
              Analytics Overview
            </h3>
            <div className="flex space-x-4">
              {options.map((option) => (
                <label
                  key={option.value}
                  className="flex items-center space-x-2"
                >
                  <Checkbox
                    checked={analyticsView.includes(option.value)}
                    onCheckedChange={(checked) =>
                      handleChange(option.value, checked as boolean)
                    }
                  />
                  <span className="text-sm text-gray-600">{option.label}</span>
                </label>
              ))}
            </div>
          </div>
          <div className="h-64 flex items-center justify-center bg-gray-50 rounded-lg">
            <p className="text-gray-500">Chart will be displayed here</p>
          </div>
        </motion.div>

        {/* Recent Articles */}
        <motion.div
          className="bg-white rounded-2xl shadow-md border border-gray-200 p-4"
          initial={{ opacity: 0, x: 20 }}
          animate={{ opacity: 1, x: 0 }}
          transition={{ delay: 0.7 }}
        >
          {/* <div className="flex justify-between items-center mb-6">
            <h3 className="text-lg font-semibold text-gray-900">
              Recent Content
            </h3>
            <Link href="/admin/content/image/create">
              <Button className="bg-gradient-to-r from-blue-500 to-purple-500 hover:from-blue-600 hover:to-purple-600 text-white shadow-lg">
                Create Content
              </Button>
            </Link>
          </div> */}

          <div className="space-y-4 max-h-96 overflow-y-auto scrollbar-thin">
            {article?.map((list: any) => (
              <motion.div
                key={list?.id}
                className="flex space-x-4 p-4 rounded-xl hover:bg-gray-50 transition-colors duration-200"
                whileHover={{ scale: 1.02 }}
                transition={{ type: "spring", stiffness: 300 }}
              >
                <Image
                  alt="thumbnail"
                  src={list?.thumbnailUrl || `/no-image.jpg`}
                  width={80}
                  height={80}
                  className="h-20 w-20 object-cover rounded-lg shadow-sm flex-shrink-0"
                />
                <div className="flex-1 min-w-0">
                  <h4 className="font-medium text-gray-900 line-clamp-2 mb-1">
                    {list?.title}
                  </h4>
                  <p className="text-sm text-gray-500">
                    {convertDateFormat(list?.createdAt)}
                  </p>
                </div>
              </motion.div>
            ))}
          </div>

          {/* <div className="mt-6 flex justify-center">
            <CustomPagination
              totalPage={totalPage}
              onPageChange={(data) => setPage(data)}
            />
          </div> */}
        </motion.div>
      </div>
    </div>
  );
}
