import React, { useState } from "react";
import {
  useDocumentDetails,
  useDocumentVersionList,
} from "../../hooks/useDocument";
import { getDocuementsStatus, useDocumentContext } from "./Documents";
import { formatUTCToLocalTime } from "../../utils/dateUtils";
import Avatar from "../common/Avatar";
import { ITEMS_PER_PAGE } from "../../utils/constants";
import Pagination from "../common/Pagination";
import VersionListSkeleton from "./VersionListSkeleton";
import DocumentDetailsSkeleton from "./DocumentDetailsSkeleton ";

const ViewDocument = () => {
  const { viewDoc, setViewDoc } = useDocumentContext();
  const [currentPage, setCurrentPage] = useState(1);
  const { data, isLoading, isError, error } = useDocumentDetails(
    viewDoc?.document
  );
  const {
    data: versionList,
    isError: isVersionError,
    isLoading: versionLoding,
    error: versionError,
  } = useDocumentVersionList(
    data?.parentAttachmentId,
    ITEMS_PER_PAGE,
    currentPage
  );
  const paginate = (page) => {
    if (page >= 1 && page <= (versionList?.totalPages ?? 1)) {
      setCurrentPage(page);
    }
  };

  if (isLoading) return <DocumentDetailsSkeleton/>;
  if (isError) return <div>{error.message}</div>;
  return (
    <div className="p-1">
      <p className="fw-bold fs-6">Document Details</p>

      {/* Row 1 */}
      {/* Row 1 */}
      <div className="row mb-2">
        <div className="col-12 col-md-6">
          <div className="d-flex text-start">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Document Name:
            </span>
            <span className="text-muted">{data.name || "-"}</span>
          </div>
        </div>
        <div className="col-12 col-md-6">
          <div className="d-flex text-start">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Document ID:
            </span>
            <span className="text-muted">{data.documentId || "-"}</span>
          </div>
        </div>
      </div>

      {/* Row 2 */}
      <div className="row mb-2">
        <div className="col-12 col-md-6 text-start">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Version:
            </span>
            <span className="text-muted">{data.version || "-"}</span>
          </div>
        </div>
        <div className="col-12 col-md-6 text-start">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Uploaded At:
            </span>
            <span className="text-muted">
              {formatUTCToLocalTime(data.uploadedAt)}
            </span>
          </div>
        </div>
      </div>

      {/* Row 3 */}
      <div className="row mb-2 text-start">
        <div className="col-12 col-md-6">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Uploaded By:
            </span>
            <span className="text-muted">
              {data.uploadedBy?.firstName || "-"}
            </span>
          </div>
        </div>
        <div className="col-12 col-md-6">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Updated At:
            </span>
            <span className="text-muted">
              {formatUTCToLocalTime(data.updatedAt) || "-"}
            </span>
          </div>
        </div>
      </div>

      {/* Row 4 */}
      <div className="row mb-2 text-start">
        <div className="col-12 col-md-6">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Category:
            </span>
            <span className="text-muted">
              {data.documentType?.documentCategory?.name || "-"}
            </span>
          </div>
        </div>
        <div className="col-12 col-md-6">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Type:
            </span>
            <span className="text-muted">{data.documentType?.name || "-"}</span>
          </div>
        </div>
      </div>

      {/* Row 5 - Tags full width */}
      <div className="row mb-2 text-start">
        <div className="col-12">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Tags:
            </span>
            <div className="d-flex flex-wrap gap-2">
              {data.tags?.length > 0 ? (
                data.tags.map((t, i) => (
                  <span
                    key={i}
                    className="badge rounded-pill bg-label-secondary"
                  >
                    {t.name}
                  </span>
                ))
              ) : (
                <span className="text-muted">-</span>
              )}
            </div>
          </div>
        </div>
      </div>

      {/* Row 6 - Description full width */}
      <div className="row mb-2 text-start">
        <div className="col-12">
          <div className="d-flex">
            <span className="fw-semibold me-2" style={{ minWidth: "130px" }}>
              Description:
            </span>
            <span className="text-muted">{data.description || "-"}</span>
          </div>
        </div>
      </div>

      <div className="row text-start py-2">
        <p className="m-0  fw-semibold : ">Documents</p>
        {versionLoding && <VersionListSkeleton items={2}/>}
        {!versionLoding &&(<div className="list-group mx-0">
          {
            versionList?.data.map((document) => (
              <a className="list-group-item list-group-item-action  py-1  border border-bottom border-top-0 border-start-0 border-end-0">
                <div className="d-flex w-100 justify-content-between m-0">
                  <p className="m-0">
                    {document.name}{" "}
                    <em className="text-secondary ms-3">
                      {formatUTCToLocalTime(document?.uploadedAt)}
                    </em>
                  </p>
                  <div className="d-flex align-items-center gap-1">
                    <small>Version {document.version}</small>
                    <small>{getDocuementsStatus(document.isVerified)}</small>
                  </div>
                </div>
                <div className="d-flex align-items-center text-secondary">
                  Upload By
                  <Avatar
                    size="xs"
                    classAvatar="m-0"
                    firstName={document.uploadedBy?.firstName}
                    lastName={document.uploadedBy?.lastName}
                  />
                  <span className="text-truncate m-0 ">
                    {`${document.uploadedBy?.firstName ?? ""} ${
                      document.uploadedBy?.lastName ?? ""
                    }`.trim() || "N/A"}
                  </span>
                </div>
                <div className="d-flex gap-2">
                  {document?.updatedAt && (
                    <span className="small text-secondary">
                      Updated At : {formatUTCToLocalTime(document.updatedAt)}
                    </span>
                  )}
                </div>
              </a>
            ))}
        </div>)}
        {!versionLoding && versionList?.data?.length > 0 && (
          <Pagination
            currentPage={currentPage}
            totalPages={versionList?.totalPages}
            onPageChange={paginate}
          />
        )}
      </div>
    </div>
  );
};

export default ViewDocument;
