'use client'

import { X } from 'lucide-react'
import { InspectorExpandButton, ResizableAside } from '../../components/ui/ResizableAside.js'
import { Toggle } from '../../components/ui/Toggle.js'
import type { PostHeaderConfig } from '../../lib/post-editor-service.js'

interface PostHeaderPanelProps {
  header: PostHeaderConfig
  canEdit: boolean
  onClose: () => void
  onChange: (patch: Partial<PostHeaderConfig>) => void
}

const LABEL = 'text-foreground mb-1 block text-xs font-medium'
const INPUT =
  'border-input bg-input-background text-foreground focus-visible:ring-ring w-full rounded-md border px-2.5 py-1.5 text-sm focus-visible:ring-2 focus-visible:outline-none'

const TOGGLES: Array<{ key: keyof PostHeaderConfig; label: string }> = [
  { key: 'showFeaturedImage', label: 'Featured image' },
  { key: 'showExcerpt', label: 'Excerpt' },
  { key: 'showAuthor', label: 'Author' },
  { key: 'showDate', label: 'Date' },
  { key: 'showCategory', label: 'Category' },
  { key: 'showReadingTime', label: 'Reading time' },
]

/** Inspector panel for the post header layout (template-level design config). */
export function PostHeaderPanel({ header, canEdit, onClose, onChange }: PostHeaderPanelProps) {
  return (
    <ResizableAside
      role="dialog"
      aria-label="Edit post header"
      className="bg-card border-border flex h-full flex-col border-l"
    >
      <div className="border-border flex items-center justify-between border-b px-6 py-4">
        <div className="min-w-0">
          <h2 className="text-foreground truncate text-sm font-medium">Post header</h2>
          <p className="text-muted-foreground text-xs">Layout for every post of this type</p>
        </div>
        <div className="flex shrink-0 items-center gap-0.5">
          <InspectorExpandButton />
          <button
            type="button"
            onClick={onClose}
            aria-label="Close header panel"
            className="text-muted-foreground hover:text-foreground hover:bg-accent flex h-9 w-9 items-center justify-center rounded-md md:h-7 md:w-7"
          >
            <X className="h-4 w-4" aria-hidden />
          </button>
        </div>
      </div>

      <div className="flex-1 space-y-6 overflow-y-auto p-4">
        <fieldset disabled={!canEdit} className="space-y-3">
          <legend className="text-muted-foreground mb-2 text-[10px] font-medium tracking-wider uppercase">
            Layout
          </legend>
          <div>
            <label className={LABEL} htmlFor="ph-layout">
              Header style
            </label>
            <select
              id="ph-layout"
              className={INPUT}
              value={header.layout}
              onChange={(e) => onChange({ layout: e.target.value as PostHeaderConfig['layout'] })}
            >
              <option value="centered">Centered</option>
              <option value="left">Left-aligned</option>
              <option value="overlay">Image overlay</option>
            </select>
          </div>
        </fieldset>

        <fieldset disabled={!canEdit} className="space-y-2">
          <legend className="text-muted-foreground mb-2 text-[10px] font-medium tracking-wider uppercase">
            Show in header
          </legend>
          {TOGGLES.map((t) => {
            const value = header[t.key] === true
            return (
              <div
                key={t.key}
                className="border-border text-foreground flex w-full items-center justify-between rounded-md border px-2.5 py-1.5 text-sm"
              >
                <span>{t.label}</span>
                <Toggle
                  checked={value}
                  disabled={!canEdit}
                  onCheckedChange={(v) => onChange({ [t.key]: v } as Partial<PostHeaderConfig>)}
                  aria-label={t.label}
                />
              </div>
            )
          })}
        </fieldset>
      </div>
    </ResizableAside>
  )
}
