import IconifyIcon from '@/components/wrappers/IconifyIcon';
import clsx from 'clsx';
import React from 'react';
import { Button } from 'react-bootstrap';

type Folder = {
  id: string;
  name: string;
  unread?: number;
};

type Props = {
  active: string;
  folders: Folder[];
  activeUnread?: number;
  onSelectFolder: (id: string) => void;
  onCompose: () => void;
  onRefresh: () => void;
  isRefreshing?: boolean;
};

const ICONS: Record<string, string> = {
  INBOX: 'solar:inbox-outline',
  Inbox: 'solar:inbox-outline',
  Drafts: 'solar:document-linear',
  Sent: 'solar:send-square-linear',
  Junk: 'solar:danger-triangle-linear',
  Trash: 'solar:trash-bin-minimalistic-outline',
  Archive: 'solar:archive-linear',
};

const normalizeId = (id: string) => (id || '').toUpperCase();

const EmailNavigationMenu: React.FC<Props> = ({
  active,
  folders,
  activeUnread = 0,
  onSelectFolder,
  onCompose,
  onRefresh,
  isRefreshing = false,
}) => {
  return (
    <div className="p-3">
      <style>{`
        .inbox-folders-title {
          margin: 0.75rem 0 0.35rem;
          text-align: left;
          font-size: 0.8rem;
          text-transform: uppercase;
          color: #6c757d;
        }

        /* --- Compose + Refresh (dark blue) --- */
        .inbox-compose-row {
          margin-bottom: 1rem;
          display: flex;
          align-items: center;
          gap: 0.5rem;
        }

        .inbox-compose-btn {
          background-color: #013254 !important;
          border-color: #013254 !important;
          color: #ffffff !important;
          font-weight: 600;
        }
        .inbox-compose-btn:hover,
        .inbox-compose-btn:focus {
          background-color: #01406f !important;
          border-color: #01406f !important;
          color: #ffffff !important;
        }

        .inbox-refresh-btn {
          background-color: #ffffff !important;
          border-color: #013254 !important;
          color: #013254 !important;
          width: 40px;
          height: 40px;
          border-radius: 999px;
          padding: 0;
          display: inline-flex;
          align-items: center;
          justify-content: center;
        }
        .inbox-refresh-btn:hover,
        .inbox-refresh-btn:focus {
          background-color: #f0f4f8 !important;
          color: #013254 !important;
        }

        .inbox-refresh-icon {
          font-size: 1.3rem; /* a bit more prominent */
        }

        .inbox-refresh-btn.is-spinning .inbox-refresh-icon {
          animation: inbox-spin 0.75s linear infinite;
        }

        @keyframes inbox-spin {
          from { transform: rotate(0deg); }
          to   { transform: rotate(360deg); }
        }

        /* --- Folder list --- */
        .inbox-folder-link {
          display: flex;
          align-items: center;
          gap: 0.5rem;
          padding: 0.35rem 0.4rem;
          border-radius: 6px;
          font-size: 0.9rem;
          cursor: pointer;
          text-decoration: none;
          color: #013254;
        }

        .inbox-folder-link:hover {
          background: rgba(1, 50, 84, 0.06);
          color: #013254;
        }

        .inbox-folder-link.active {
          background: rgba(1, 50, 84, 0.12);
          font-weight: 600;
          color: #013254;
        }

        .inbox-folder-icon {
          color: #013254 !important;
          font-size: 1.1rem; /* slightly larger */
        }

        .inbox-folder-label {
          color: #013254;
        }

        .inbox-folder-count {
          margin-left: auto;
          font-size: 0.8rem;
          min-width: 1.5rem;
          text-align: right;
          color: #013254;
        }
      `}</style>

      {/* Compose + refresh row */}
      <div className="inbox-compose-row">
        <Button
          className="inbox-compose-btn flex-grow-1"
          onClick={onCompose}
        >
          Compose
        </Button>

        <Button
          type="button"
          className={clsx('inbox-refresh-btn', {
            'is-spinning': isRefreshing,
          })}
          onClick={onRefresh}
          disabled={isRefreshing}
        >
          <IconifyIcon
            icon="solar:refresh-outline"
            className="inbox-refresh-icon"
          />
          <span className="visually-hidden">Refresh</span>
        </Button>
      </div>

      <div className="inbox-folders-title fw-semibold">Folders</div>
      <div className="d-flex flex-column gap-1">
        {folders.map((f) => {
          const id = f.id || f.name;
          const isActive = normalizeId(id) === normalizeId(active);

          const unread =
            typeof f.unread === 'number'
              ? f.unread
              : 0;

          const iconKey = ICONS[id] || ICONS[normalizeId(id)] || ICONS.Inbox;

          return (
            <div
              key={id}
              className={clsx('inbox-folder-link', { active: isActive })}
              onClick={() => onSelectFolder(id)}
            >
              <IconifyIcon icon={iconKey} className="inbox-folder-icon" />
              <span className="inbox-folder-label text-truncate">{f.name}</span>
              {unread > 0 && (
                <span className="inbox-folder-count">{unread}</span>
              )}
            </div>
          );
        })}
      </div>
    </div>
  );
};

export default EmailNavigationMenu;
