-- Minimal offices / memberships / folders for docv API (zones 5 & 2 subset).
-- user_uid matches users primary key value (UUID as text from JWT sub) without DB FK (supports id or uid PK on users).

CREATE TABLE IF NOT EXISTS offices (
    uid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name TEXT NOT NULL,
    siren TEXT,
    address TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS office_members (
    office_uid UUID NOT NULL REFERENCES offices(uid) ON DELETE CASCADE,
    user_uid UUID NOT NULL,
    role TEXT NOT NULL DEFAULT 'member',
    PRIMARY KEY (office_uid, user_uid)
);

CREATE INDEX IF NOT EXISTS idx_office_members_user ON office_members(user_uid);

CREATE TABLE IF NOT EXISTS folders (
    uid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    office_uid UUID NOT NULL REFERENCES offices(uid) ON DELETE CASCADE,
    title TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'open',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_folders_office ON folders(office_uid);
