CREATE TABLE `calls` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int,
	`callerName` varchar(255),
	`callerPhone` varchar(64) NOT NULL,
	`direction` enum('inbound','outbound') NOT NULL DEFAULT 'inbound',
	`status` enum('answered','missed','voicemail','busy') NOT NULL DEFAULT 'answered',
	`duration` int DEFAULT 0,
	`assignedAgentId` int,
	`notes` text,
	`recordingUrl` varchar(512),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `calls_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `company_settings` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int NOT NULL,
	`companyName` varchar(255),
	`companyEmail` varchar(320),
	`companyPhone` varchar(64),
	`companyAddress` text,
	`website` varchar(512),
	`timezone` varchar(64) DEFAULT 'UTC',
	`language` varchar(8) DEFAULT 'fr',
	`whatsappEnabled` boolean NOT NULL DEFAULT true,
	`emailEnabled` boolean NOT NULL DEFAULT true,
	`chatEnabled` boolean NOT NULL DEFAULT true,
	`callsEnabled` boolean NOT NULL DEFAULT true,
	`emailNotifications` boolean NOT NULL DEFAULT true,
	`smsNotifications` boolean NOT NULL DEFAULT false,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `company_settings_id` PRIMARY KEY(`id`),
	CONSTRAINT `company_settings_clientId_unique` UNIQUE(`clientId`)
);
--> statement-breakpoint
CREATE TABLE `emails` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int,
	`fromName` varchar(255) NOT NULL,
	`fromEmail` varchar(320) NOT NULL,
	`toEmail` varchar(320),
	`subject` varchar(512) NOT NULL,
	`body` text NOT NULL,
	`replyBody` text,
	`status` enum('unread','read','replied','archived') NOT NULL DEFAULT 'unread',
	`category` enum('support','sales','billing','technical','general') NOT NULL DEFAULT 'general',
	`priority` enum('low','normal','high','urgent') NOT NULL DEFAULT 'normal',
	`assignedAgentId` int,
	`repliedAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `emails_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `invoices` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int NOT NULL,
	`subscriptionId` int,
	`invoiceNumber` varchar(64) NOT NULL,
	`amount` decimal(10,2) NOT NULL,
	`currency` varchar(8) NOT NULL DEFAULT 'USD',
	`status` enum('paid','pending','failed','refunded') NOT NULL DEFAULT 'pending',
	`description` varchar(512),
	`paidAt` timestamp,
	`dueDate` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `invoices_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `subscription_plans` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(64) NOT NULL,
	`displayName` varchar(128) NOT NULL,
	`priceMonthly` decimal(10,2) NOT NULL,
	`priceYearly` decimal(10,2) NOT NULL,
	`maxAgents` int NOT NULL DEFAULT 1,
	`maxConversations` int NOT NULL DEFAULT 100,
	`features` text,
	`isActive` boolean NOT NULL DEFAULT true,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `subscription_plans_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `subscriptions` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int NOT NULL,
	`planId` int NOT NULL,
	`planName` varchar(64) NOT NULL,
	`status` enum('active','cancelled','expired','trial') NOT NULL DEFAULT 'trial',
	`billingCycle` enum('monthly','yearly') NOT NULL DEFAULT 'monthly',
	`amount` decimal(10,2) NOT NULL,
	`currency` varchar(8) NOT NULL DEFAULT 'USD',
	`startDate` timestamp NOT NULL DEFAULT (now()),
	`renewalDate` timestamp NOT NULL,
	`cancelledAt` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `subscriptions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `web_chat_messages` (
	`id` int AUTO_INCREMENT NOT NULL,
	`chatId` int NOT NULL,
	`senderType` enum('agent','visitor','system') NOT NULL DEFAULT 'visitor',
	`senderName` varchar(255),
	`content` text NOT NULL,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `web_chat_messages_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `web_chats` (
	`id` int AUTO_INCREMENT NOT NULL,
	`clientId` int,
	`visitorName` varchar(255) NOT NULL,
	`visitorEmail` varchar(320),
	`status` enum('active','waiting','closed','archived') NOT NULL DEFAULT 'waiting',
	`category` enum('support','sales','billing','technical','general') NOT NULL DEFAULT 'general',
	`assignedAgentId` int,
	`lastMessage` text,
	`lastMessageAt` timestamp DEFAULT (now()),
	`unreadCount` int NOT NULL DEFAULT 0,
	`pageUrl` varchar(512),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `web_chats_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `leads` MODIFY COLUMN `source` enum('whatsapp','call','web','email','chat','referral','other') NOT NULL DEFAULT 'whatsapp';--> statement-breakpoint
ALTER TABLE `daily_stats` ADD `emailsCount` int DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `daily_stats` ADD `chatsCount` int DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `leads` ADD `company` varchar(255);