CREATE TABLE `agents` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`email` varchar(320),
	`phone` varchar(64),
	`status` enum('online','offline','busy') NOT NULL DEFAULT 'offline',
	`callsHandled` int NOT NULL DEFAULT 0,
	`messagesHandled` int NOT NULL DEFAULT 0,
	`conversionRate` decimal(5,2) DEFAULT '0.00',
	`clientId` int,
	`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 TABLE `clients` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`industry` varchar(128),
	`contact` varchar(255),
	`phone` varchar(64),
	`email` varchar(320),
	`status` enum('active','inactive','trial') NOT NULL DEFAULT 'active',
	`subscription` enum('starter','pro','enterprise') NOT NULL DEFAULT 'starter',
	`userId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `clients_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `conversations` (
	`id` int AUTO_INCREMENT NOT NULL,
	`contactName` varchar(255) NOT NULL,
	`contactPhone` varchar(64),
	`channel` enum('whatsapp','call','sms') NOT NULL DEFAULT 'whatsapp',
	`status` enum('open','qualified','completed','archived') NOT NULL DEFAULT 'open',
	`assignedAgentId` int,
	`clientId` int,
	`leadId` int,
	`lastMessage` text,
	`lastMessageAt` timestamp DEFAULT (now()),
	`unreadCount` int NOT NULL DEFAULT 0,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `conversations_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `daily_stats` (
	`id` int AUTO_INCREMENT NOT NULL,
	`date` varchar(16) NOT NULL,
	`callsCount` int NOT NULL DEFAULT 0,
	`messagesCount` int NOT NULL DEFAULT 0,
	`leadsCount` int NOT NULL DEFAULT 0,
	`clientId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `daily_stats_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `leads` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`phone` varchar(64),
	`email` varchar(320),
	`source` enum('whatsapp','call','web','referral','other') NOT NULL DEFAULT 'whatsapp',
	`interest` text,
	`status` enum('New','Qualified','Converted','Lost') NOT NULL DEFAULT 'New',
	`assignedAgentId` int,
	`clientId` int,
	`notes` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `leads_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `messages` (
	`id` int AUTO_INCREMENT NOT NULL,
	`conversationId` int NOT NULL,
	`senderType` enum('agent','contact','system') NOT NULL DEFAULT 'contact',
	`senderName` varchar(255),
	`content` text NOT NULL,
	`messageType` enum('text','image','audio','document') NOT NULL DEFAULT 'text',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `messages_id` PRIMARY KEY(`id`)
);
