CREATE TABLE `SIPInboxes` (
	`id` text PRIMARY KEY NOT NULL,
	`organization_id` text NOT NULL,
	`max_outbound_calls` integer DEFAULT 1 NOT NULL,
	`domain` text NOT NULL,
	`port` integer DEFAULT 5060 NOT NULL,
	`trunks` text NOT NULL,
	`feature_map` text NOT NULL,
	`branches` text NOT NULL,
	`dids` text DEFAULT '[]' NOT NULL,
	`inbounds` text NOT NULL,
	`time_conditions` text NOT NULL,
	`musics` text NOT NULL,
	`announcements` text NOT NULL,
	`outbounds` text NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Accounts` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`type` text NOT NULL,
	`provider` text NOT NULL,
	`provider_account_id` text NOT NULL,
	`refresh_token` text,
	`access_token` text,
	`expires_at` integer,
	`token_type` text,
	`scope` text,
	`id_token` text,
	`session_state` text,
	FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Attachments` (
	`id` text PRIMARY KEY NOT NULL,
	`url` text NOT NULL,
	`type` text DEFAULT 'IMAGE' NOT NULL,
	`message_id` text,
	`contact_id` text NOT NULL,
	FOREIGN KEY (`message_id`) REFERENCES `Messages`(`id`) ON UPDATE cascade ON DELETE set null,
	FOREIGN KEY (`contact_id`) REFERENCES `Contacts`(`id`) ON UPDATE cascade ON DELETE restrict
);
--> statement-breakpoint
CREATE TABLE `_ContactToConversation` (
	`contact_id` text NOT NULL,
	`conversation_id` text NOT NULL,
	FOREIGN KEY (`contact_id`) REFERENCES `Contacts`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`conversation_id`) REFERENCES `Conversations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `_ContactToTag` (
	`A` text NOT NULL,
	`B` text NOT NULL,
	FOREIGN KEY (`A`) REFERENCES `Contacts`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`B`) REFERENCES `Tags`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Contacts` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`phone_number` text,
	`email` text,
	`company` text,
	`country` text,
	`city` text,
	`address` text,
	`user_id` text,
	`identifier` text NOT NULL,
	`organization_id` text NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `_ConversationToTag` (
	`conversation_id` text NOT NULL,
	`tag_id` text NOT NULL,
	FOREIGN KEY (`conversation_id`) REFERENCES `Conversations`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`tag_id`) REFERENCES `Tags`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Conversations` (
	`id` text PRIMARY KEY NOT NULL,
	`protocol` text NOT NULL,
	`status` text DEFAULT 'OPEN' NOT NULL,
	`priority` text DEFAULT 'NORMAL' NOT NULL,
	`inbox_type` text DEFAULT 'INTERNAL' NOT NULL,
	`text_enabled` integer DEFAULT true NOT NULL,
	`attachment_enabled` integer DEFAULT true NOT NULL,
	`voice_enabled` integer DEFAULT true NOT NULL,
	`video_enabled` integer DEFAULT true NOT NULL,
	`user_owner_id` text,
	`organization_id` text NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`user_owner_id`) REFERENCES `Contacts`(`id`) ON UPDATE cascade ON DELETE set null,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Flows` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`steps` text,
	`organization_id` text NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `InviteTokens` (
	`id` text PRIMARY KEY NOT NULL,
	`email` text NOT NULL,
	`token` text NOT NULL,
	`used` integer DEFAULT false NOT NULL,
	`organization_id` text NOT NULL,
	`expires` integer NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Messages` (
	`id` text PRIMARY KEY NOT NULL,
	`content` text NOT NULL,
	`status` text DEFAULT 'QUEUED' NOT NULL,
	`error_message` text,
	`from_system` integer DEFAULT false NOT NULL,
	`from_id` text NOT NULL,
	`conversation_id` text NOT NULL,
	`deleted` numeric,
	`edited` numeric,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`from_id`) REFERENCES `Contacts`(`id`) ON UPDATE cascade ON DELETE restrict,
	FOREIGN KEY (`conversation_id`) REFERENCES `Conversations`(`id`) ON UPDATE cascade ON DELETE restrict
);
--> statement-breakpoint
CREATE TABLE `_OrganizationToUser` (
	`A` text NOT NULL,
	`B` text NOT NULL,
	FOREIGN KEY (`A`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`B`) REFERENCES `Users`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Organizations` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`nick_name` text NOT NULL,
	`cnpj` integer NOT NULL,
	`avatar_url` text,
	`country` text DEFAULT 'São Paulo' NOT NULL,
	`state` text DEFAULT 'São Paulo' NOT NULL,
	`street` text DEFAULT '' NOT NULL,
	`email` text DEFAULT '' NOT NULL,
	`phone_number_a` text DEFAULT '' NOT NULL,
	`phone_number_b` text DEFAULT '' NOT NULL,
	`active` integer DEFAULT true NOT NULL,
	`max_user` integer DEFAULT 2 NOT NULL,
	`language` text DEFAULT 'PT' NOT NULL,
	`max_messenger_inbox` integer DEFAULT 0 NOT NULL,
	`max_email_inbox` integer DEFAULT 0 NOT NULL,
	`max_whatsapp_cloud_inbox` integer DEFAULT 0 NOT NULL,
	`max_whatsapp_web_inbox` integer DEFAULT 0 NOT NULL,
	`max_sms_inbox` integer DEFAULT 0 NOT NULL,
	`max_sip_inbox` integer DEFAULT 0 NOT NULL,
	`max_sip_outbound_calls` integer DEFAULT 1 NOT NULL,
	`max_api_inbox` integer DEFAULT 0 NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `PasswordResetTokens` (
	`id` text PRIMARY KEY NOT NULL,
	`email` text NOT NULL,
	`token` text NOT NULL,
	`expires` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `Pauses` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`organization_id` text NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Sessions` (
	`id` text PRIMARY KEY NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`user_id` text NOT NULL,
	`cookie_data` text NOT NULL,
	`device` text DEFAULT 'unknown',
	`address` text DEFAULT 'unknown',
	FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Tags` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`description` text,
	`hex_color` text DEFAULT '#f3ff3f' NOT NULL,
	`organization_id` text NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `_TeamToUser` (
	`team_id` text NOT NULL,
	`user_id` text NOT NULL,
	FOREIGN KEY (`team_id`) REFERENCES `Teams`(`id`) ON UPDATE cascade ON DELETE cascade,
	FOREIGN KEY (`user_id`) REFERENCES `Users`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `Teams` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`description` text,
	`organization_id` text NOT NULL,
	FOREIGN KEY (`organization_id`) REFERENCES `Organizations`(`id`) ON UPDATE cascade ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `TwoFactorTokens` (
	`id` text PRIMARY KEY NOT NULL,
	`email` text NOT NULL,
	`token` text NOT NULL,
	`expires` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `Users` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text,
	`email` text NOT NULL,
	`email_verified` numeric,
	`image` text,
	`password` text,
	`role` text DEFAULT 'AGENT' NOT NULL,
	`paused_status_id` text,
	`paused_at` numeric,
	`unpaused_at` numeric,
	`is_two_factor_enabled` integer DEFAULT false NOT NULL,
	`active` integer DEFAULT true NOT NULL,
	`auto_answer` integer DEFAULT false NOT NULL,
	`allow_support_access` integer DEFAULT 1717600680836 NOT NULL,
	`created_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	`updated_at` numeric DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
	FOREIGN KEY (`paused_status_id`) REFERENCES `Pauses`(`id`) ON UPDATE cascade ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `VerificationTokens` (
	`id` text PRIMARY KEY NOT NULL,
	`email` text NOT NULL,
	`token` text NOT NULL,
	`used` integer DEFAULT false NOT NULL,
	`expires` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `SIPInboxes_id_key` ON `SIPInboxes` (`id`);--> statement-breakpoint
CREATE UNIQUE INDEX `SIPInboxes_domain_key` ON `SIPInboxes` (`domain`);--> statement-breakpoint
CREATE INDEX `SIPInboxes_organization_id_idx` ON `SIPInboxes` (`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `accounts_provider_provider_account_id_key` ON `Accounts` (`provider`,`provider_account_id`);--> statement-breakpoint
CREATE INDEX `_ContactToConversation_conversation_id_index` ON `_ContactToConversation` (`conversation_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `_ContactToConversation_contact_id_conversation_id_unique` ON `_ContactToConversation` (`contact_id`,`conversation_id`);--> statement-breakpoint
CREATE INDEX `_ContactToTag_B_index` ON `_ContactToTag` (`B`);--> statement-breakpoint
CREATE UNIQUE INDEX `_ContactToTag_AB_unique` ON `_ContactToTag` (`A`,`B`);--> statement-breakpoint
CREATE UNIQUE INDEX `Contact_identifier_organization_id_key` ON `Contacts` (`identifier`,`organization_id`);--> statement-breakpoint
CREATE INDEX `Contact_organization_id_idx` ON `Contacts` (`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Contact_id_key` ON `Contacts` (`id`);--> statement-breakpoint
CREATE INDEX `_ConversationToTag_tag_id_index` ON `_ConversationToTag` (`tag_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `_ConversationToTag_conversation_id_tag_id_unique` ON `_ConversationToTag` (`conversation_id`,`tag_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Conversations_protocol_unique` ON `Conversations` (`protocol`);--> statement-breakpoint
CREATE INDEX `Conversation_protocol_idx` ON `Conversations` (`protocol`);--> statement-breakpoint
CREATE INDEX `Conversation_user_owner_id_idx` ON `Conversations` (`user_owner_id`);--> statement-breakpoint
CREATE INDEX `Conversation_organization_id_idx` ON `Conversations` (`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Conversation_protocol_key` ON `Conversations` (`protocol`);--> statement-breakpoint
CREATE UNIQUE INDEX `InviteTokens_token_unique` ON `InviteTokens` (`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `InviteToken_email_token_key` ON `InviteTokens` (`email`,`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `InviteToken_token_key` ON `InviteTokens` (`token`);--> statement-breakpoint
CREATE INDEX `Message_from_id_idx` ON `Messages` (`from_id`);--> statement-breakpoint
CREATE INDEX `_OrganizationToUser_B_index` ON `_OrganizationToUser` (`B`);--> statement-breakpoint
CREATE UNIQUE INDEX `_OrganizationToUser_AB_unique` ON `_OrganizationToUser` (`A`,`B`);--> statement-breakpoint
CREATE UNIQUE INDEX `Organization_id_key` ON `Organizations` (`id`);--> statement-breakpoint
CREATE UNIQUE INDEX `PasswordResetToken_email_token_key` ON `PasswordResetTokens` (`email`,`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `PasswordResetToken_token_key` ON `PasswordResetTokens` (`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `Pauses_name_organizationId_key` ON `Pauses` (`name`,`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `sessions_id_key` ON `Sessions` (`id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Tag_name_organization_id_key` ON `Tags` (`name`,`organization_id`);--> statement-breakpoint
CREATE INDEX `Tag_organization_id_idx` ON `Tags` (`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Tag_id_key` ON `Tags` (`id`);--> statement-breakpoint
CREATE INDEX `_TeamToUser_user_id_index` ON `_TeamToUser` (`user_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `_TeamToUser_team_id_user_id_unique` ON `_TeamToUser` (`team_id`,`user_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Team_name_organization_id_key` ON `Teams` (`name`,`organization_id`);--> statement-breakpoint
CREATE INDEX `Team_organization_id_idx` ON `Teams` (`organization_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `Team_id_key` ON `Teams` (`id`);--> statement-breakpoint
CREATE UNIQUE INDEX `TwoFactorToken_email_token_key` ON `TwoFactorTokens` (`email`,`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `Users_email_unique` ON `Users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_key` ON `Users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `VerificationToken_email_token_key` ON `VerificationTokens` (`email`,`token`);--> statement-breakpoint
CREATE UNIQUE INDEX `VerificationToken_token_key` ON `VerificationTokens` (`token`);