-- Migration 006: Approval Gates Proposals
-- Creates tables for task approval proposals and reviews

-- Proposals table (risk-based approval gates)
DEFINE TABLE proposals SCHEMAFULL
    PERMISSIONS
        FOR select WHERE tenant_id = $auth.tenant_id
        FOR create, update, delete WHERE tenant_id = $auth.tenant_id;

DEFINE FIELD id ON TABLE proposals TYPE record<proposals>;
DEFINE FIELD tenant_id ON TABLE proposals TYPE string ASSERT $value != NONE;
DEFINE FIELD project_id ON TABLE proposals TYPE string ASSERT $value != NONE;
DEFINE FIELD task_id ON TABLE proposals TYPE string ASSERT $value != NONE;
DEFINE FIELD agent_id ON TABLE proposals TYPE string ASSERT $value != NONE;
DEFINE FIELD title ON TABLE proposals TYPE string ASSERT $value != NONE AND string::len($value) > 0;
DEFINE FIELD description ON TABLE proposals TYPE option<string>;
DEFINE FIELD status ON TABLE proposals TYPE string ASSERT $value INSIDE ["proposed", "approved", "rejected", "executed"] DEFAULT "proposed";
DEFINE FIELD risk_level ON TABLE proposals TYPE string ASSERT $value INSIDE ["low", "medium", "high"] DEFAULT "low";
DEFINE FIELD plan_details ON TABLE proposals TYPE object {
    description: string,
    affected_resources: array<string>,
    estimated_cost: option<number>,
    confidence: number,
    rollback_strategy: option<string>,
    metadata: object
};
DEFINE FIELD created_at ON TABLE proposals TYPE datetime DEFAULT time::now();
DEFINE FIELD submitted_at ON TABLE proposals TYPE option<datetime>;
DEFINE FIELD reviewed_at ON TABLE proposals TYPE option<datetime>;
DEFINE FIELD executed_at ON TABLE proposals TYPE option<datetime>;

DEFINE INDEX idx_proposals_tenant ON TABLE proposals COLUMNS tenant_id;
DEFINE INDEX idx_proposals_project ON TABLE proposals COLUMNS project_id;
DEFINE INDEX idx_proposals_task ON TABLE proposals COLUMNS task_id;
DEFINE INDEX idx_proposals_status ON TABLE proposals COLUMNS status;
DEFINE INDEX idx_proposals_risk ON TABLE proposals COLUMNS risk_level;
DEFINE INDEX idx_proposals_tenant_project ON TABLE proposals COLUMNS tenant_id, project_id;
DEFINE INDEX idx_proposals_tenant_status ON TABLE proposals COLUMNS tenant_id, status;
DEFINE INDEX idx_proposals_created ON TABLE proposals COLUMNS created_at;

-- Proposal reviews table (feedback and approvals)
DEFINE TABLE proposal_reviews SCHEMAFULL;

DEFINE FIELD id ON TABLE proposal_reviews TYPE record<proposal_reviews>;
DEFINE FIELD proposal_id ON TABLE proposal_reviews TYPE string ASSERT $value != NONE;
DEFINE FIELD reviewer_id ON TABLE proposal_reviews TYPE string ASSERT $value != NONE;
DEFINE FIELD feedback ON TABLE proposal_reviews TYPE string ASSERT $value != NONE;
DEFINE FIELD approved ON TABLE proposal_reviews TYPE bool DEFAULT false;
DEFINE FIELD created_at ON TABLE proposal_reviews TYPE datetime DEFAULT time::now();

DEFINE INDEX idx_reviews_proposal ON TABLE proposal_reviews COLUMNS proposal_id;
DEFINE INDEX idx_reviews_reviewer ON TABLE proposal_reviews COLUMNS reviewer_id;
DEFINE INDEX idx_reviews_created ON TABLE proposal_reviews COLUMNS created_at;
