'use client';

import { useEffect, useState } from 'react';
import { CrmModal, ModalHeader } from '@/components/crm/FormDrawer';
import { useToast } from '@/components/crm/ToastProvider';
import type { Frequency } from '@/features/workflows/analyzeBuckets';

export interface ChartSettings {
  frequency: Frequency;
  kind: 'line' | 'bar';
  values: boolean;
}

export const DEFAULT_CHART: ChartSettings = { frequency: 'daily', kind: 'line', values: false };

export const FREQUENCY_PHRASE: Record<Frequency, string> = {
  daily: 'day by day',
  weekly: 'week by week',
  monthly: 'month by month',
};

/** A chart's edit button (design: "Chart options" on the over-time charts). */
export function ChartOptionsButton({ onClick }: { onClick: () => void }) {
  return (
    <button
      type="button"
      className="icon-btn"
      aria-label="Chart options"
      title="Chart options"
      onClick={onClick}
    >
      <svg
        className="icon iconSize-18"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="1.8"
        strokeLinecap="round"
        strokeLinejoin="round"
        aria-hidden
      >
        <path d="M4 6h16M7 12h10M10 18h4" />
      </svg>
    </button>
  );
}

/**
 * The chart's drawer, with HubSpot's Data frequency (sheet row 67). `settings` of the chart
 * being edited, or null when closed. It stands outside the cards, over the page.
 */
export function ChartOptionsDrawer({
  settings,
  onClose,
  onApply,
}: {
  settings: ChartSettings | null;
  onClose: () => void;
  onApply: (next: ChartSettings) => void;
}) {
  const toast = useToast();
  const [draft, setDraft] = useState<ChartSettings>(DEFAULT_CHART);
  useEffect(() => {
    if (settings) setDraft(settings);
  }, [settings]);
  const set = (patch: Partial<ChartSettings>) => setDraft((d) => ({ ...d, ...patch }));
  const id = 'wfChartOptions';

  return (
    <CrmModal id={id} drawer open={settings !== null} onClose={onClose}>
      <ModalHeader id={id} title="Chart options" onClose={onClose} />
      <form
        onSubmit={(e) => {
          e.preventDefault();
          onApply(draft);
          onClose();
          toast('Chart updated');
        }}
      >
        <div className="modal__body">
          <div className="form-grid">
            <label className="field">
              <span className="field__label">Data frequency</span>
              <select
                className="select"
                value={draft.frequency}
                onChange={(e) => set({ frequency: e.target.value as Frequency })}
              >
                <option value="daily">Daily</option>
                <option value="weekly">Weekly</option>
                <option value="monthly">Monthly</option>
              </select>
            </label>
            <label className="field">
              <span className="field__label">Display as</span>
              <select
                className="select"
                value={draft.kind}
                onChange={(e) => set({ kind: e.target.value as ChartSettings['kind'] })}
              >
                <option value="line">Line chart</option>
                <option value="bar">Bar chart</option>
              </select>
            </label>
            <label className="check">
              <input
                type="checkbox"
                checked={draft.values}
                onChange={(e) => set({ values: e.target.checked })}
              />
              <span className="check__box" aria-hidden />
              Show data labels
            </label>
          </div>
        </div>
        <div className="modal__footer">
          <button type="button" className="btn-crm btn-crm--outline" onClick={onClose}>
            Cancel
          </button>
          <button type="submit" className="btn-crm btn-crm--primary">
            Apply
          </button>
        </div>
      </form>
    </CrmModal>
  );
}
