"use client";

import { CheckIcon, CrossIcon, UserIcon } from "@/assets/icons";
import Status from "@/components/ui/Status";
import { useUser } from "@/contexts";
import { formatPhoneNumber, unixToDate } from "@/utils/shared";
import Image from "next/image";
import { useState } from "react";

export default function Page() {
  const { user: data } = useUser();
  const [imageError, setImageError] = useState(false);

  const hasProfileImage = data?.profile_image && !imageError;

  return (
    <div className="mx-auto w-full max-w-4xl rounded-lg bg-white p-6 shadow-default dark:bg-gray-dark">
      <div className="relative h-48 w-full rounded-t-lg bg-gray-200 dark:bg-gray-700">
        <Image
          src={"/images/cover/cover-01.png"}
          alt="Cover"
          layout="fill"
          objectFit="cover"
          className="rounded-t-lg"
        />
        <div className="absolute bottom-0 left-1/2 -translate-x-1/2 translate-y-1/2 transform">
          <div className="relative flex h-32 w-32 items-center justify-center overflow-hidden rounded-full border-4 border-white bg-gray-200 dark:border-gray-dark dark:bg-gray-700">
            {hasProfileImage ? (
              <Image
                onError={() => setImageError(true)}
                alt="Profile"
                src={data.profile_image!}
                layout="fill"
                objectFit="cover"
                className="rounded-full"
              />
            ) : (
              <UserIcon className="h-16 w-16 text-gray-400 dark:text-gray-500" />
            )}
          </div>
        </div>
      </div>

      <div className="mt-16 text-center">
        <h1 className="text-2xl font-bold text-dark dark:text-white">
          {data?.full_name}
        </h1>
        <p className="text-sm text-gray-500 dark:text-gray-400">
          @{data?.username}
        </p>
      </div>

      <div className="mt-6 border-t border-stroke pt-6 dark:border-stroke-dark">
        <h2 className="mb-4 text-lg font-semibold text-dark dark:text-white">
          User Information
        </h2>
        <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div>
            <p className="font-medium text-dark dark:text-white">Email</p>
            <p className="text-gray-600 dark:text-gray-300">{data?.email}</p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Phone</p>
            <p className="text-gray-600 dark:text-gray-300">
              {formatPhoneNumber(data?.phone)}
            </p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Department</p>
            <p className="text-gray-600 dark:text-gray-300">
              {data?.department}
            </p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Position</p>
            <p className="text-gray-600 dark:text-gray-300">{data?.position}</p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Role</p>
            <p className="capitalize text-gray-600 dark:text-gray-300">
              {data?.role}
            </p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Status</p>
            <div className="w-fit">
              <Status status={data?.status}>{data?.status}</Status>
            </div>
          </div>
        </div>
      </div>

      <div className="mt-6 border-t border-stroke pt-6 dark:border-stroke-dark">
        <h2 className="mb-4 text-lg font-semibold text-dark dark:text-white">
          Account Details
        </h2>
        <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
          <div>
            <p className="font-medium text-dark dark:text-white">Verified</p>
            <div className="w-fit">
              <Status status={data?.is_verified ? "success" : "warning"}>
                {data?.is_verified ? (
                  <CheckIcon fill="green" width={20} height={20} />
                ) : (
                  <CrossIcon fill="red" width={20} />
                )}
              </Status>
            </div>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">Last Login</p>
            <p className="text-gray-600 dark:text-gray-300">
              {unixToDate({ unix: data?.last_login_at ?? 0, hasTime: true })}
            </p>
          </div>
          <div>
            <p className="font-medium text-dark dark:text-white">
              Member Since
            </p>
            <p className="text-gray-600 dark:text-gray-300">
              {unixToDate({ unix: data?.created_at ?? 0, hasTime: true })}
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}
