CREATE TABLE `agentAccessControl` (
	`id` int AUTO_INCREMENT NOT NULL,
	`agentId` int NOT NULL,
	`tool` varchar(50) NOT NULL,
	`isAllowed` boolean DEFAULT true,
	`maxExecutionsPerHour` int DEFAULT 100,
	`timeoutSeconds` int DEFAULT 30,
	`allowedPatterns` json DEFAULT ('[]'),
	`blockedPatterns` json DEFAULT ('[]'),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `agentAccessControl_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `agentHistory` (
	`id` int AUTO_INCREMENT NOT NULL,
	`agentId` int NOT NULL,
	`userMessage` text NOT NULL,
	`agentResponse` text,
	`conversationId` varchar(64),
	`messageIndex` int,
	`status` enum('pending','success','error') DEFAULT 'pending',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `agentHistory_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `agentMetrics` (
	`id` int AUTO_INCREMENT NOT NULL,
	`agentId` int NOT NULL,
	`requestId` varchar(64) NOT NULL,
	`userMessage` text,
	`agentResponse` text,
	`inputTokens` int DEFAULT 0,
	`outputTokens` int DEFAULT 0,
	`totalTokens` int DEFAULT 0,
	`processingTimeMs` int NOT NULL,
	`status` enum('success','error','timeout','rate_limited') NOT NULL,
	`errorMessage` text,
	`toolsCalled` json DEFAULT ('[]'),
	`model` varchar(100),
	`temperature` decimal(3,2),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `agentMetrics_id` PRIMARY KEY(`id`),
	CONSTRAINT `agentMetrics_requestId_unique` UNIQUE(`requestId`)
);
--> statement-breakpoint
CREATE TABLE `agents` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`name` varchar(255) NOT NULL,
	`description` text,
	`role` varchar(100) NOT NULL,
	`model` varchar(100) NOT NULL,
	`provider` varchar(50) NOT NULL,
	`temperature` decimal(3,2) DEFAULT '0.7',
	`maxTokens` int DEFAULT 2048,
	`topP` decimal(3,2) DEFAULT '1.0',
	`frequencyPenalty` decimal(3,2) DEFAULT '0.0',
	`presencePenalty` decimal(3,2) DEFAULT '0.0',
	`systemPrompt` text,
	`allowedTools` json DEFAULT ('[]'),
	`allowedDomains` json DEFAULT ('[]'),
	`maxRequestsPerHour` int DEFAULT 100,
	`isActive` boolean DEFAULT true,
	`isPublic` boolean DEFAULT false,
	`tags` json DEFAULT ('[]'),
	`metadata` json DEFAULT ('{}'),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `agents_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE INDEX `agentAccessControl_agentId_tool_idx` ON `agentAccessControl` (`agentId`,`tool`);--> statement-breakpoint
CREATE INDEX `agentHistory_agentId_idx` ON `agentHistory` (`agentId`);--> statement-breakpoint
CREATE INDEX `agentMetrics_agentId_idx` ON `agentMetrics` (`agentId`);--> statement-breakpoint
CREATE INDEX `agentMetrics_createdAt_idx` ON `agentMetrics` (`createdAt`);--> statement-breakpoint
CREATE INDEX `agents_userId_idx` ON `agents` (`userId`);--> statement-breakpoint
CREATE INDEX `agents_model_idx` ON `agents` (`model`);