'use client';

import { useRouter } from 'next/navigation';
import { FoxButton } from '@fox/core/components/fox-button';
import { FoxButtonGroup } from '@fox/core/components/fox-button-group';
import { FoxIconButton } from '@fox/core/components/fox-icon-button';
import { useFeedback } from '@/app/_hooks/use-feedback';
import { useBlockingAction } from '@/app/_hooks/use-blocking-action';
import type { ContentKind } from '@/lib/domain/content';
import { contentEditPath } from '@/lib/domain/content-query';
import { deleteContentAction } from '../_actions';

interface ContentRowActionsProps {
  kind: ContentKind;
  id: string;
  name: string;
}

/** 목록 행의 「관리」 — 수정은 등록과 같은 폼 화면으로 이동하고, 삭제는 얼럿으로 확인한다. */
export function ContentRowActions({ kind, id, name }: ContentRowActionsProps) {
  const router = useRouter();
  const { showAlert, hideAlert, showToast } = useFeedback();
  const [isDeleting, startDeleting] = useBlockingAction();

  function runDelete() {
    hideAlert();
    startDeleting(async () => {
      const result = await deleteContentAction(kind, id);
      if (result.status === 'error') {
        showToast({
          variant: 'danger',
          message: result.message ?? '삭제하지 못했습니다.',
        });
        return;
      }
      showToast({ variant: 'success', message: '콘텐츠를 삭제했습니다.' });
    });
  }

  function confirmDelete() {
    showAlert({
      variant: 'danger',
      title: '콘텐츠를 삭제하시겠습니까?',
      message: `${name}을(를) 삭제합니다. 삭제 후에는 되돌릴 수 없습니다.`,
      actions: (
        <>
          <FoxButton type="default" size="md" label="취소" onAction={hideAlert} />
          <FoxButton type="primary" size="md" label="삭제" onAction={runDelete} />
        </>
      ),
    });
  }

  return (
    <FoxButtonGroup size="sm">
      <FoxIconButton
        type="default"
        size="sm"
        icon={<i className="fox-ico fox-ico-PencilSimple" aria-hidden="true" />}
        label="수정"
        onAction={() => router.push(contentEditPath(kind, id))}
      />
      <FoxIconButton
        type="default"
        size="sm"
        icon={<i className="fox-ico fox-ico-Trash" aria-hidden="true" />}
        label={isDeleting ? '삭제 중' : '삭제'}
        disabled={isDeleting}
        onAction={confirmDelete}
      />
    </FoxButtonGroup>
  );
}
