Table plans {
  id bigserial [pk]
  guid uuid [unique, not null, default: `gen_random_uuid()`]
  title text [not null]
  description text [not null, default: '']
  status text [not null, default: 'draft']       // draft, active, blocked, completed, cancelled, superseded
  priority text [not null, default: 'medium']    // low, medium, high, critical
  project_id bigint [ref: > projects.id]
  owner text
  due_date timestamptz
  completed_at timestamptz
  reviewed_by text
  last_reviewed_at timestamptz
  supersedes_plan_id bigint [ref: > plans.id]
  tags "text[]" [not null, default: `'{}'`]
  created_at timestamptz [not null, default: `now()`]
  updated_at timestamptz [not null, default: `now()`]

  indexes {
    project_id
    status
    priority
    owner
    due_date
    last_reviewed_at
    tags [type: gin]
    title [type: gin]
  }
}

// Directional: plan_id cannot proceed until depends_on_plan_id is complete
Table plan_dependencies {
  id bigserial [pk]
  plan_id bigint [not null, ref: > plans.id]
  depends_on_plan_id bigint [not null, ref: > plans.id]
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (plan_id, depends_on_plan_id) [unique]
    plan_id
    depends_on_plan_id
  }
}

// Bidirectional: store with plan_a_id < plan_b_id to avoid duplicates
Table plan_related_plans {
  id bigserial [pk]
  plan_a_id bigint [not null, ref: > plans.id]
  plan_b_id bigint [not null, ref: > plans.id]
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (plan_a_id, plan_b_id) [unique]
    plan_a_id
    plan_b_id
  }
}

Table plan_skills {
  id bigserial [pk]
  plan_id bigint [not null, ref: > plans.id]
  skill_id bigint [not null, ref: > agent_skills.id]
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (plan_id, skill_id) [unique]
    plan_id
  }
}

Table plan_guardrails {
  id bigserial [pk]
  plan_id bigint [not null, ref: > plans.id]
  guardrail_id bigint [not null, ref: > agent_guardrails.id]
  created_at timestamptz [not null, default: `now()`]

  indexes {
    (plan_id, guardrail_id) [unique]
    plan_id
  }
}

// Cross-file refs (for relspecgo merge)
Ref: plans.project_id > projects.id [delete: set null]
Ref: plans.supersedes_plan_id > plans.id [delete: set null]
Ref: plan_dependencies.plan_id > plans.id [delete: cascade]
Ref: plan_dependencies.depends_on_plan_id > plans.id [delete: cascade]
Ref: plan_related_plans.plan_a_id > plans.id [delete: cascade]
Ref: plan_related_plans.plan_b_id > plans.id [delete: cascade]
Ref: plan_skills.plan_id > plans.id [delete: cascade]
Ref: plan_skills.skill_id > agent_skills.id [delete: cascade]
Ref: plan_guardrails.plan_id > plans.id [delete: cascade]
Ref: plan_guardrails.guardrail_id > agent_guardrails.id [delete: cascade]
