CREATE TABLE "ai_call_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"tenant_id" uuid NOT NULL,
	"ticket_id" uuid,
	"processing_run_id" uuid,
	"agent_name" text NOT NULL,
	"provider" text NOT NULL,
	"model" text NOT NULL,
	"prompt_tokens" integer,
	"completion_tokens" integer,
	"total_tokens" integer,
	"estimated_cost_usd" numeric(10, 6),
	"latency_ms" integer NOT NULL,
	"success" boolean NOT NULL,
	"error_code" text,
	"error_message" text,
	"used_fallback" boolean DEFAULT false NOT NULL,
	"own_api_key" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "ai_call_logs" ADD CONSTRAINT "ai_call_logs_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_call_logs" ADD CONSTRAINT "ai_call_logs_ticket_id_tickets_id_fk" FOREIGN KEY ("ticket_id") REFERENCES "public"."tickets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "ai_call_logs" ADD CONSTRAINT "ai_call_logs_processing_run_id_ticket_processing_runs_id_fk" FOREIGN KEY ("processing_run_id") REFERENCES "public"."ticket_processing_runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "ai_call_logs_tenant_created_at_idx" ON "ai_call_logs" USING btree ("tenant_id","created_at");--> statement-breakpoint
CREATE INDEX "ai_call_logs_ticket_id_idx" ON "ai_call_logs" USING btree ("ticket_id");--> statement-breakpoint
CREATE INDEX "ai_call_logs_agent_success_created_at_idx" ON "ai_call_logs" USING btree ("agent_name","success","created_at");