-- Add foreign key constraints to existing tables
-- This links all data to the owners table
-- NOTE: This migration depends on placeholder owners existing (migration 005)

-- Files table
ALTER TABLE files
    ADD CONSTRAINT fk_files_owner
        FOREIGN KEY (owner_id) REFERENCES owners(id);

-- Tasks table
ALTER TABLE tasks
    ADD CONSTRAINT fk_tasks_owner
        FOREIGN KEY (owner_id) REFERENCES owners(id);

-- Daemons table
ALTER TABLE daemons
    ADD CONSTRAINT fk_daemons_owner
        FOREIGN KEY (owner_id) REFERENCES owners(id);

-- Mesh chat conversations table
ALTER TABLE mesh_chat_conversations
    ADD CONSTRAINT fk_mesh_chat_conversations_owner
        FOREIGN KEY (owner_id) REFERENCES owners(id);

-- Verify indexes exist (they should from original migrations)
-- If not, add them for query performance:
CREATE INDEX IF NOT EXISTS idx_files_owner_id ON files(owner_id);
CREATE INDEX IF NOT EXISTS idx_tasks_owner_id ON tasks(owner_id);
CREATE INDEX IF NOT EXISTS idx_daemons_owner_id ON daemons(owner_id);
CREATE INDEX IF NOT EXISTS idx_mesh_chat_conversations_owner_id ON mesh_chat_conversations(owner_id);
