-- Wever Labs Runtime Data Binding v1
-- Supabase/Postgres migration scaffold for binding public agent-facing contracts to durable OS records.

create extension if not exists pgcrypto;

create table if not exists agent_onboarding_records (
  id uuid primary key default gen_random_uuid(),
  onboarding_id text unique not null,
  agent_id text,
  manifest_url text,
  trust_status text default 'pending_review',
  status_url text,
  quote_path_url text,
  run_contract_path_url text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_trust_profiles (
  id uuid primary key default gen_random_uuid(),
  trust_profile_id text unique not null,
  agent_id text not null,
  trust_state text not null default 'pending_review',
  allowed_tools text[] not null default '{}',
  callback_url text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_credential_envelopes (
  id uuid primary key default gen_random_uuid(),
  envelope_id text unique not null,
  agent_id text not null,
  trust_profile_id text,
  key_scope text[] not null default '{}',
  delivery_status text not null default 'queued',
  delivery_channel text,
  expires_at timestamptz,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_quote_records (
  id uuid primary key default gen_random_uuid(),
  quote_id text unique not null,
  agent_id text,
  requested_tool text not null,
  workflow_type text,
  quoted_credits numeric,
  quote_state text not null default 'issued',
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists settlement_provider_routes (
  id uuid primary key default gen_random_uuid(),
  route_id text unique not null,
  provider text not null,
  agent_id text,
  quote_id text,
  settlement_id text,
  route_state text not null default 'created',
  confirmation_status text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_credit_entitlements (
  id uuid primary key default gen_random_uuid(),
  credit_entitlement_id text unique not null,
  agent_id text,
  quote_id text,
  settlement_id text,
  credits_granted numeric not null default 0,
  credits_remaining numeric not null default 0,
  entitlement_state text not null default 'active',
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_run_contracts (
  id uuid primary key default gen_random_uuid(),
  contract_id text unique not null,
  agent_id text,
  requested_tool text not null,
  workflow_type text,
  contract_state text not null default 'active',
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_task_runs (
  id uuid primary key default gen_random_uuid(),
  task_token text unique not null,
  agent_id text,
  requested_tool text not null,
  workflow_type text,
  contract_id text,
  quote_id text,
  credit_entitlement_id text,
  canonical_state text not null default 'queued',
  evidence_score numeric,
  result_contract_id text,
  callback_id text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_lifecycle_events (
  id uuid primary key default gen_random_uuid(),
  event_id text unique not null,
  task_token text not null,
  previous_state text,
  next_state text not null,
  source text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create table if not exists agent_result_contracts (
  id uuid primary key default gen_random_uuid(),
  result_contract_id text unique not null,
  task_token text not null,
  contract_id text,
  result_state text not null default 'result_ready',
  machine_result jsonb not null default '{}'::jsonb,
  human_summary text,
  proof_objects jsonb not null default '{}'::jsonb,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_settlement_receipts (
  id uuid primary key default gen_random_uuid(),
  settlement_receipt_id text unique not null,
  settlement_id text,
  provider text,
  agent_id text,
  quote_id text,
  credit_entitlement_id text,
  receipt_state text not null default 'issued',
  amount numeric,
  asset text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create table if not exists agent_usage_receipts (
  id uuid primary key default gen_random_uuid(),
  usage_receipt_id text unique not null,
  task_token text not null,
  agent_id text,
  requested_tool text,
  credits_consumed numeric not null default 0,
  receipt_state text not null default 'issued',
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create table if not exists agent_callback_deliveries (
  id uuid primary key default gen_random_uuid(),
  callback_id text unique not null,
  task_token text not null,
  agent_id text,
  callback_url text,
  delivery_state text not null default 'queued',
  attempts integer not null default 0,
  last_attempt_at timestamptz,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists agent_result_attestations (
  id uuid primary key default gen_random_uuid(),
  attestation_id text unique not null,
  task_token text not null,
  result_contract_id text,
  attestation_state text not null default 'issued',
  payload_hash text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create table if not exists runtime_binding_events (
  id uuid primary key default gen_random_uuid(),
  event_id text unique not null,
  task_token text,
  record_id text,
  record_type text not null,
  event_type text not null,
  previous_state text,
  next_state text,
  source text,
  payload jsonb not null default '{}'::jsonb,
  created_at timestamptz not null default now()
);

create index if not exists idx_agent_task_runs_state on agent_task_runs(canonical_state);
create index if not exists idx_agent_task_runs_tool on agent_task_runs(requested_tool);
create index if not exists idx_agent_lifecycle_events_task on agent_lifecycle_events(task_token);
create index if not exists idx_agent_usage_receipts_task on agent_usage_receipts(task_token);
create index if not exists idx_agent_callback_deliveries_task on agent_callback_deliveries(task_token);
create index if not exists idx_runtime_binding_events_task on runtime_binding_events(task_token);
