-- Ensure document filenames are unique per tenant + folder
DROP INDEX IF EXISTS documents_tenant_folder_filename_unique;
DROP INDEX IF EXISTS documents_unique_folder_filename;
CREATE UNIQUE INDEX documents_tenant_folder_filename_unique
    ON documents (
        tenant_id,
        COALESCE(folder_id, '00000000-0000-0000-0000-000000000000'::uuid),
        filename
    )
    WHERE deleted_at IS NULL;

-- Ensure tag labels are unique per tenant
ALTER TABLE tags DROP CONSTRAINT IF EXISTS tags_label_key;
DROP INDEX IF EXISTS tags_tenant_label_unique;
CREATE UNIQUE INDEX tags_tenant_label_unique ON tags (tenant_id, label);

-- Ensure correspondent names are unique per tenant
ALTER TABLE correspondents DROP CONSTRAINT IF EXISTS correspondents_name_unique;
DROP INDEX IF EXISTS correspondents_tenant_name_unique;
CREATE UNIQUE INDEX correspondents_tenant_name_unique ON correspondents (tenant_id, name);
