-- Société (office) : hiérarchie, archivage ; dossier : prolonge le DP ; commentaires société (socle SPEC_18).

ALTER TABLE offices ADD COLUMN IF NOT EXISTS parent_office_uid UUID REFERENCES offices(uid) ON DELETE SET NULL;
ALTER TABLE offices ADD COLUMN IF NOT EXISTS archived_at TIMESTAMPTZ;

CREATE INDEX IF NOT EXISTS idx_offices_parent ON offices(parent_office_uid) WHERE parent_office_uid IS NOT NULL;

ALTER TABLE folders ADD COLUMN IF NOT EXISTS extends_permanent_record BOOLEAN NOT NULL DEFAULT false;

CREATE TABLE IF NOT EXISTS office_comments (
    uid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    office_uid UUID NOT NULL REFERENCES offices(uid) ON DELETE CASCADE,
    user_uid UUID NOT NULL,
    content TEXT NOT NULL,
    access_level TEXT NOT NULL DEFAULT 'internal',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_office_comments_office ON office_comments(office_uid);
