import { useEffect, useState } from 'react'
import {
classifyRecordError,
fetchClientArchivePreview,
setClientArchived,
type ClientArchivePreview,
type RecordFailureKind,
} from '@faunosteo/supabase'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import Notice from '@/components/ui/Notice'
import { getSupabaseClient } from '@/lib/supabase'
import { useT } from '@/i18n/useT'
export interface ArchiveClientDialogProps {
open: boolean
clientId: string
onClose: () => void
/** Called once the server archived the client; the page then closes the dialog. */
onArchived: () => void
}
type Preview =
| { status: 'loading' }
| { status: 'ready'; preview: ClientArchivePreview }
| { status: 'failed' }
/**
* Confirmation before archiving (contract ยง7.1): it lists the active animals
* for which this client is the only current owner, then archives through
* set_client_archived. Closing it is never a confirmation, and while the
* request runs it refuses to close, so focus is restored once, after it.
*/
export default function ArchiveClientDialog({
open,
clientId,
onClose,
onArchived,
}: ArchiveClientDialogProps) {
const t = useT()
const [preview, setPreview] = useState
{t('clients.archive.preview_loading')}
)} {preview.status === 'failed' && ({sole.count === 1 ? t('clients.archive.sole_owner_one') : t('clients.archive.sole_owner_other').replace( '{count}', String(sole.count), )}
{t('clients.archive.others').replace('{count}', String(others))}
)}