// components/BackgroundAudio.jsx
import React from 'react';
import { Music, Crown, X } from 'lucide-react';
import { isPro } from '../utils/shortcode';

/**
 * Background audio for one PDF (Pro): pick an audio file from the Media
 * Library; it gets a play/pause button in the viewer.
 * value: { id, title, url, autoplay } | null
 */
const BackgroundAudio = ({ value, onChange }) => {
    const pro = isPro();

    const pick = () => {
        const frame = window.wp.media({
            title: 'Background audio',
            button: { text: 'Use this audio' },
            library: { type: 'audio' },
            multiple: false,
        });
        frame.on('select', () => {
            const a = frame.state().get('selection').first().toJSON();
            onChange({ id: a.id, title: a.title || a.filename, url: a.url, autoplay: value?.autoplay ?? false });
        });
        frame.open();
    };

    return (
        <div className="rounded-xl border border-slate-200 p-4">
            <div className="flex items-start gap-3">
                <span className={`mt-0.5 rounded-lg p-1.5 ${value ? 'bg-indigo-50 text-indigo-600' : 'bg-slate-100 text-slate-400'}`}>
                    <Music className="size-4" />
                </span>
                <div className="min-w-0 flex-1">
                    <p className="flex items-center gap-1.5 text-sm font-medium text-slate-800">
                        Background audio
                        {!pro && (
                            <span className="inline-flex items-center gap-0.5 rounded-full bg-amber-100 px-1.5 py-px text-[10px] font-bold uppercase text-amber-700">
                                <Crown className="size-3" />Pro
                            </span>
                        )}
                    </p>
                    <p className="text-xs text-slate-500 mt-0.5">Music or narration that readers can play while reading. Shows a play/pause button on the viewer.</p>

                    <div className={`mt-3 ${!pro ? 'opacity-50 pointer-events-none' : ''}`}>
                        {value ? (
                            <div className="space-y-2">
                                <div className="flex items-center gap-2 rounded-lg bg-slate-50 px-3 py-2">
                                    <Music className="size-4 shrink-0 text-indigo-500" />
                                    <span className="min-w-0 flex-1 truncate text-sm text-slate-800" title={value.title}>{value.title}</span>
                                    <button type="button" onClick={pick} className="text-xs font-semibold text-indigo-600 hover:text-indigo-800">Change</button>
                                    <button type="button" onClick={() => onChange(null)} className="p-1 rounded text-slate-400 hover:text-red-600" aria-label="Remove background audio">
                                        <X className="size-3.5" />
                                    </button>
                                </div>
                                {value.url && <audio src={value.url} controls preload="none" className="w-full h-8" />}
                                <label className="flex items-center gap-2 text-xs text-slate-600">
                                    <input
                                        type="checkbox"
                                        checked={!!value.autoplay}
                                        onChange={(e) => onChange({ ...value, autoplay: e.target.checked })}
                                        className="size-4 appearance-auto accent-indigo-600 cursor-pointer"
                                        style={{ padding: 0 }}
                                    />
                                    Start playing when the reader first clicks in the viewer
                                </label>
                            </div>
                        ) : (
                            <button type="button" onClick={pick} className="inline-flex items-center gap-1.5 rounded-lg border border-slate-200 bg-white px-3 py-1.5 text-xs font-semibold text-slate-700 hover:border-indigo-300 hover:text-indigo-700">
                                <Music className="size-3.5" /> Choose audio
                            </button>
                        )}
                    </div>
                    {!pro && window.pdfRack?.upgradeUrl && (
                        <a href={window.pdfRack.upgradeUrl} className="mt-2 inline-block text-xs font-semibold text-amber-700 hover:text-amber-900">Upgrade to Pro →</a>
                    )}
                </div>
            </div>
        </div>
    );
};

export default BackgroundAudio;
