import { Check, Minus, X } from 'lucide-react'
import { OPTIONS, BANDCAMP_FREEDOM, type Freedom, type FreedomFact, type FreedomLevel } from './model'

// Level → glyph + color, encoded redundantly (glyph + text + color, never hue alone).
const LEVEL: Record<FreedomLevel, { Icon: typeof Check; cls: string; sr: string }> = {
  good: { Icon: Check, cls: 'text-ready', sr: 'yes' },
  mixed: { Icon: Minus, cls: 'text-status-wip', sr: 'partial' },
  bad: { Icon: X, cls: 'text-blocked', sr: 'no' },
}

const COLS: { key: keyof Freedom; label: string }[] = [
  { key: 'remaster', label: 'Remaster (swap audio)' },
  { key: 'edit', label: 'Edit metadata' },
  { key: 'pull', label: 'Pull / takedown' },
  { key: 'unpaid', label: 'If you stop paying' },
]

function Cell({ fact }: { fact: FreedomFact }) {
  const { Icon, cls, sr } = LEVEL[fact.level]
  return (
    <td className="border-t border-hairline px-3 py-2.5 align-top">
      <div className="flex gap-2">
        <Icon size={14} className={`mt-px shrink-0 ${cls}`} aria-hidden />
        <span className="sr-only">{sr}: </span>
        <span className="text-[12px] leading-snug text-ink-muted">{fact.text}</span>
      </div>
    </td>
  )
}

/** Rows = the four distributors + the Bandcamp layer; columns = the freedom axes
 *  that actually differ (rights ownership doesn't — see the note below). */
export function FreedomMatrix() {
  const rows = [
    ...OPTIONS.map((o) => ({ id: o.id, name: o.name, recommended: o.recommended, freedom: o.freedom })),
    { id: 'bandcamp', name: 'Bandcamp (layer)', recommended: false, freedom: BANDCAMP_FREEDOM },
  ]
  return (
    <section className="rounded-lg border border-hairline bg-raised p-4">
      <div className="mb-3 flex flex-wrap items-baseline justify-between gap-2">
        <h2 className="text-sm font-semibold text-ink">Freedom &amp; control</h2>
        <p className="max-w-prose text-xs text-ink-muted">
          Can you fix, change, or walk away, and at what cost? You keep 100% of your masters with all of
          them — these are the axes that actually differ.
        </p>
      </div>

      <div className="rounded-md border border-hairline bg-surface p-3 text-xs leading-relaxed text-ink-muted">
        <span className="font-medium text-ink">No DSP distributor swaps audio in place</span> — that&apos;s a
        store rule, not the distributor&apos;s. Everywhere except DistroKid Ultimate, a remaster is a brand-new
        release (new ISRC, lost release date &amp; plays). The exception worth knowing:{' '}
        <span className="text-melodic">Bandcamp lets you replace the file freely, anytime</span> — reason
        enough to keep it as your master-of-record.
      </div>

      <div className="mt-3 -mx-1 overflow-x-auto">
        <table className="w-full min-w-[640px] border-collapse text-left">
          <thead>
            <tr>
              <th className="px-3 pb-2 text-[11px] font-medium uppercase tracking-wide text-ink-faint">
                Option
              </th>
              {COLS.map((c) => (
                <th
                  key={c.key}
                  className="px-3 pb-2 text-[11px] font-medium uppercase tracking-wide text-ink-faint"
                >
                  {c.label}
                </th>
              ))}
            </tr>
          </thead>
          <tbody>
            {rows.map((r) => (
              <tr key={r.id} className={r.recommended ? 'bg-magenta/[0.04]' : undefined}>
                <th
                  scope="row"
                  className="border-t border-hairline px-3 py-2.5 align-top text-[12px] font-medium text-ink"
                >
                  {r.name}
                </th>
                {COLS.map((c) => (
                  <Cell key={c.key} fact={r.freedom[c.key]} />
                ))}
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      <p className="mt-3 text-[11px] leading-snug text-ink-faint">
        <span className="text-ink-muted">Ownership ≠ permanence.</span> All of these are non-exclusive: your
        masters and copyrights stay 100% yours. What varies is <span className="text-ink-muted">permanence</span>{' '}
        (does it stay up unpaid), <span className="text-ink-muted">edit-freedom</span> (can you fix it), and{' '}
        <span className="text-ink-muted">portability</span> (you keep your ISRCs and can re-deliver elsewhere).
        Fine print: CD Baby Pro (legacy) and TuneCore publishing-admin take a cut of <em>publishing</em>{' '}
        royalties — opt-in, separate from distribution, never your masters.
      </p>
    </section>
  )
}
