-- Create proposals table for symbiosis proposals between organizations
CREATE TABLE IF NOT EXISTS proposals (
    id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,
    from_org_id TEXT NOT NULL,
    to_org_id TEXT NOT NULL,
    resource_id TEXT NOT NULL,
    resource_type VARCHAR(50) NOT NULL,
    resource_name TEXT,
    message TEXT,
    status VARCHAR(50) DEFAULT 'pending',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

    -- Foreign key constraints
    FOREIGN KEY (from_org_id) REFERENCES organizations(id) ON DELETE CASCADE,
    FOREIGN KEY (to_org_id) REFERENCES organizations(id) ON DELETE CASCADE,
    FOREIGN KEY (resource_id) REFERENCES resource_flows(id) ON DELETE CASCADE
);

-- Create indexes for performance
CREATE INDEX IF NOT EXISTS idx_proposals_from_org_id ON proposals(from_org_id);
CREATE INDEX IF NOT EXISTS idx_proposals_to_org_id ON proposals(to_org_id);
CREATE INDEX IF NOT EXISTS idx_proposals_resource_id ON proposals(resource_id);
CREATE INDEX IF NOT EXISTS idx_proposals_status ON proposals(status);
CREATE INDEX IF NOT EXISTS idx_proposals_created_at ON proposals(created_at DESC);
