-- Add tenant_id to documents
ALTER TABLE documents ADD COLUMN tenant_id UUID;
UPDATE documents
SET tenant_id = (SELECT tenant_id FROM public.tenants WHERE slug = 'admin');
ALTER TABLE documents ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE documents
    ADD CONSTRAINT documents_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX documents_tenant_id_idx ON documents (tenant_id);

-- Add tenant_id to document_versions
ALTER TABLE document_versions ADD COLUMN tenant_id UUID;
UPDATE document_versions AS dv
SET tenant_id = d.tenant_id
FROM documents AS d
WHERE dv.document_id = d.id;
ALTER TABLE document_versions ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE document_versions
    ADD CONSTRAINT document_versions_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX document_versions_tenant_id_idx ON document_versions (tenant_id);

-- Add tenant_id to document_assets
ALTER TABLE document_assets ADD COLUMN tenant_id UUID;
UPDATE document_assets AS da
SET tenant_id = dv.tenant_id
FROM document_versions AS dv
WHERE da.document_version_id = dv.id;
ALTER TABLE document_assets ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE document_assets
    ADD CONSTRAINT document_assets_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX document_assets_tenant_id_idx ON document_assets (tenant_id);

-- Add tenant_id to document_asset_objects
ALTER TABLE document_asset_objects ADD COLUMN tenant_id UUID;
UPDATE document_asset_objects AS dao
SET tenant_id = da.tenant_id
FROM document_assets AS da
WHERE dao.asset_id = da.id;
ALTER TABLE document_asset_objects ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE document_asset_objects
    ADD CONSTRAINT document_asset_objects_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX document_asset_objects_tenant_id_idx ON document_asset_objects (tenant_id);

-- Add tenant_id to document_tags
ALTER TABLE document_tags ADD COLUMN tenant_id UUID;
UPDATE document_tags AS dt
SET tenant_id = d.tenant_id
FROM documents AS d
WHERE dt.document_id = d.id;
ALTER TABLE document_tags ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE document_tags
    ADD CONSTRAINT document_tags_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX document_tags_tenant_id_idx ON document_tags (tenant_id);

-- Add tenant_id to document_correspondents
ALTER TABLE document_correspondents ADD COLUMN tenant_id UUID;
UPDATE document_correspondents AS dc
SET tenant_id = d.tenant_id
FROM documents AS d
WHERE dc.document_id = d.id;
ALTER TABLE document_correspondents ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE document_correspondents
    ADD CONSTRAINT document_correspondents_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX document_correspondents_tenant_id_idx ON document_correspondents (tenant_id);

-- Add tenant_id to correspondents
ALTER TABLE correspondents ADD COLUMN tenant_id UUID;
UPDATE correspondents
SET tenant_id = (SELECT tenant_id FROM public.tenants WHERE slug = 'admin');
ALTER TABLE correspondents ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE correspondents
    ADD CONSTRAINT correspondents_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX correspondents_tenant_id_idx ON correspondents (tenant_id);

-- Add tenant_id to tags
ALTER TABLE tags ADD COLUMN tenant_id UUID;
UPDATE tags
SET tenant_id = (SELECT tenant_id FROM public.tenants WHERE slug = 'admin');
ALTER TABLE tags ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE tags
    ADD CONSTRAINT tags_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX tags_tenant_id_idx ON tags (tenant_id);

-- Add tenant_id to folders
ALTER TABLE folders ADD COLUMN tenant_id UUID;
UPDATE folders
SET tenant_id = (SELECT tenant_id FROM public.tenants WHERE slug = 'admin');
ALTER TABLE folders ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE folders
    ADD CONSTRAINT folders_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX folders_tenant_id_idx ON folders (tenant_id);

-- Add tenant_id to users
ALTER TABLE users ADD COLUMN tenant_id UUID;
-- Add tenant_id to refresh_tokens
ALTER TABLE refresh_tokens ADD COLUMN tenant_id UUID;
UPDATE refresh_tokens AS rt
SET tenant_id = (
    SELECT um.tenant_id
    FROM user_memberships AS um
    WHERE um.user_id = rt.user_id
    LIMIT 1
);
ALTER TABLE refresh_tokens ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE refresh_tokens
    ADD CONSTRAINT refresh_tokens_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX refresh_tokens_tenant_id_idx ON refresh_tokens (tenant_id);

-- Add tenant_id to jobs
ALTER TABLE jobs ADD COLUMN tenant_id UUID;
UPDATE jobs
SET tenant_id = (
    SELECT um.tenant_id
    FROM user_memberships AS um
    WHERE um.user_id = jobs.user_id
    LIMIT 1
);
ALTER TABLE jobs ALTER COLUMN tenant_id SET NOT NULL;
ALTER TABLE jobs
    ADD CONSTRAINT jobs_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenants(tenant_id);
CREATE INDEX jobs_tenant_id_idx ON jobs (tenant_id);
