-- Generated by drizzle-kit, then trimmed. Three statements it emitted are not
-- here, and deleting them is the point rather than an oversight.
--
-- 0005, 0012 and 0013 were hand-written and left no snapshot, so drizzle
-- diffed against 0011 and re-proposed their work: `ALTER TYPE pig_team_role
-- ADD VALUE 'viewer'` (0012, and without the IF NOT EXISTS that made that one
-- safe) and a drop-and-add of the learn_resources provider CHECK (0013). On
-- any database those migrations have already touched, the first fails outright
-- and the second is a needless drop of a live constraint. The 0014 snapshot
-- beside this file is kept in full, because it describes the schema as it
-- actually is — which is what re-synchronises the snapshot chain with the
-- hand-written migrations for whoever generates 0015.
--
-- Everything below is additive: two new tables and one new nullable column.
-- Nothing here reads or rewrites an existing row, so it cannot fail on data.
CREATE TABLE "piggy_conversations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"title" text NOT NULL,
	"model" text,
	"mode" text,
	"read_capability" text DEFAULT 'book:read' NOT NULL,
	"context" jsonb,
	"last_message_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "piggy_conversations_mode_check" CHECK ("piggy_conversations"."mode" IS NULL OR "piggy_conversations"."mode" IN ('read_only', 'confirm', 'auto')),
	CONSTRAINT "piggy_conversations_read_capability_check" CHECK ("piggy_conversations"."read_capability" IN ('book:read', 'economics:read', 'team:read')),
	CONSTRAINT "piggy_conversations_title_check" CHECK (length("piggy_conversations"."title") > 0)
);
--> statement-breakpoint
CREATE TABLE "piggy_messages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"conversation_id" uuid NOT NULL,
	"seq" integer NOT NULL,
	"role" text NOT NULL,
	"content" text DEFAULT '' NOT NULL,
	"reasoning" text,
	"model" text,
	"mode" text,
	"input_tokens" integer,
	"output_tokens" integer,
	"cost_micro_cents" integer,
	"finish_reason" text,
	"tool_call_id" text,
	"tool_name" text,
	"tool_arguments" jsonb,
	"tool_result" jsonb,
	"tool_ok" boolean,
	"approval_id" text,
	"approval_change" jsonb,
	"approval_decision" text,
	"approval_decided_at" timestamp with time zone,
	"error" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "piggy_messages_role_check" CHECK ("piggy_messages"."role" IN ('user', 'assistant', 'tool')),
	CONSTRAINT "piggy_messages_mode_check" CHECK ("piggy_messages"."mode" IS NULL OR "piggy_messages"."mode" IN ('read_only', 'confirm', 'auto')),
	CONSTRAINT "piggy_messages_approval_decision_check" CHECK ("piggy_messages"."approval_decision" IS NULL OR "piggy_messages"."approval_decision" IN ('apply', 'reject')),
	CONSTRAINT "piggy_messages_seq_check" CHECK ("piggy_messages"."seq" >= 0)
);
--> statement-breakpoint
ALTER TABLE "agent_runs" ADD COLUMN "piggy_conversation_id" uuid;--> statement-breakpoint
ALTER TABLE "piggy_conversations" ADD CONSTRAINT "piggy_conversations_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "piggy_messages" ADD CONSTRAINT "piggy_messages_conversation_id_piggy_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."piggy_conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_runs" ADD CONSTRAINT "agent_runs_piggy_conversation_id_piggy_conversations_id_fk" FOREIGN KEY ("piggy_conversation_id") REFERENCES "public"."piggy_conversations"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "piggy_conversations_user_recent_idx" ON "piggy_conversations" USING btree ("user_id","last_message_at" DESC NULLS LAST);--> statement-breakpoint
CREATE UNIQUE INDEX "piggy_messages_conversation_seq_key" ON "piggy_messages" USING btree ("conversation_id","seq");--> statement-breakpoint
CREATE INDEX "agent_runs_piggy_conversation_idx" ON "agent_runs" USING btree ("piggy_conversation_id");
