-- Create owners table for multi-tenant support
-- Owners are the logical entities that own resources (files, tasks, daemons)
-- Users belong to owners via groups

CREATE TABLE IF NOT EXISTS owners (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name VARCHAR(255) NOT NULL,
    -- 'personal' for individual users, 'organization' for teams
    owner_type VARCHAR(32) NOT NULL DEFAULT 'personal',
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    CONSTRAINT valid_owner_type CHECK (owner_type IN ('personal', 'organization'))
);

CREATE INDEX idx_owners_owner_type ON owners(owner_type);

-- Trigger to update updated_at timestamp (reuse existing function)
CREATE TRIGGER update_owners_updated_at
    BEFORE UPDATE ON owners
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

COMMENT ON TABLE owners IS 'Logical entities that own resources. Users access owners via group membership.';
COMMENT ON COLUMN owners.owner_type IS 'Type of owner: personal (single user) or organization (team)';
