-- Documents rattachés à un dossier (lecture côté fiche dossier ; CRUD via API).

CREATE TABLE IF NOT EXISTS folder_documents (
    uid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    folder_uid UUID NOT NULL REFERENCES folders(uid) ON DELETE CASCADE,
    name TEXT NOT NULL,
    doc_type TEXT NOT NULL DEFAULT 'autre',
    category TEXT NOT NULL DEFAULT 'dossier',
    uploaded_by TEXT NOT NULL CHECK (uploaded_by IN ('cabinet', 'client')),
    size_label TEXT NOT NULL DEFAULT '—',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    CONSTRAINT folder_documents_doc_type_chk CHECK (
        doc_type IN (
            'kbis', 'statuts', 'pv_ag', 'pacte_associes', 'contrat', 'facture', 'autre'
        )
    ),
    CONSTRAINT folder_documents_category_chk CHECK (
        category IN ('permanent', 'dossier')
    )
);

CREATE INDEX IF NOT EXISTS idx_folder_documents_folder ON folder_documents(folder_uid);
