-- CreateTable
CREATE TABLE `users` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `password` VARCHAR(128) NOT NULL,
    `email` VARCHAR(191) NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `users_email_key`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `profiles` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(128) NOT NULL,
    `acls` VARCHAR(191) NOT NULL DEFAULT '',
    `dialPlanGroup_id` INTEGER NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `tariffs` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(191) NOT NULL,
    `domain_id` INTEGER NOT NULL,
    `rule_id` INTEGER NOT NULL,
    `tariff_per_call` BOOLEAN NOT NULL DEFAULT false,
    `markup` VARCHAR(191) NOT NULL DEFAULT '0.00',
    `plan_type` VARCHAR(191) NOT NULL DEFAULT 'postpaid',
    `closing_type` VARCHAR(191) NOT NULL DEFAULT 'transaction',
    `rounding_type` VARCHAR(191) NOT NULL DEFAULT 'truncate',
    `purchase_connection_fee` VARCHAR(191) NOT NULL DEFAULT '',
    `purchase_cadence` INTEGER NOT NULL DEFAULT 0,
    `initial_delay` INTEGER NOT NULL DEFAULT 0,
    `disposal_fee` VARCHAR(191) NOT NULL DEFAULT '',
    `connection_value` VARCHAR(191) NOT NULL DEFAULT '',
    `quota_limit` VARCHAR(191) NOT NULL DEFAULT '',
    `call_0800` VARCHAR(12) NULL,
    `call_0300` VARCHAR(12) NULL,
    `call_0500` VARCHAR(12) NULL,
    `call_special_number` VARCHAR(12) NULL,
    `call_emergency_numbers` VARCHAR(12) NULL,
    `call_local_numbers` VARCHAR(12) NULL,
    `call_ldn_numbers` VARCHAR(12) NULL,
    `call_local_mobile_numbers` VARCHAR(12) NULL,
    `call_mobile_numbers_ldn_vc2` VARCHAR(12) NULL,
    `call_mobile_numbers_ldn_vc3` VARCHAR(12) NULL,
    `call_local_numbers_reduced_value` VARCHAR(12) NULL,
    `call_international_numbers` VARCHAR(12) NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `clients` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `company_name` VARCHAR(191) NOT NULL,
    `fantasy_name` VARCHAR(191) NOT NULL,
    `email` VARCHAR(64) NOT NULL,
    `identifier` VARCHAR(191) NOT NULL,
    `profile_id` INTEGER NOT NULL,
    `tariff_id` INTEGER NOT NULL,
    `record_calls` BOOLEAN NOT NULL DEFAULT false,
    `max_calls_outbound` INTEGER NOT NULL DEFAULT 3,
    `contract_number` VARCHAR(191) NOT NULL,
    `cpf_cnpj` VARCHAR(191) NOT NULL,
    `address_number` VARCHAR(191) NOT NULL,
    `address_neighborhood` VARCHAR(191) NOT NULL,
    `address_street` VARCHAR(191) NOT NULL,
    `address_state` VARCHAR(191) NOT NULL,
    `address_city` VARCHAR(191) NOT NULL,
    `cep` VARCHAR(191) NOT NULL,
    `telphone` VARCHAR(191) NOT NULL,
    `notification_low_balance` BOOLEAN NOT NULL DEFAULT false,
    `limite_type` VARCHAR(191) NOT NULL DEFAULT 'monetary',
    `minute_limit_day` VARCHAR(191) NOT NULL DEFAULT '',
    `minute_limit_month` VARCHAR(191) NOT NULL DEFAULT '',
    `cnl` VARCHAR(191) NOT NULL,
    `carried` BOOLEAN NOT NULL DEFAULT false,
    `area_code` INTEGER NOT NULL,
    `country_code` INTEGER NOT NULL,
    `call_limit_receptive` INTEGER NOT NULL DEFAULT 0,
    `call_limit_carried_out` INTEGER NOT NULL DEFAULT 0,
    `status` VARCHAR(191) NOT NULL DEFAULT 'active',
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `domain` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensip_domain_id` INTEGER NOT NULL,
    `domain` VARCHAR(191) NOT NULL,
    `attrs` VARCHAR(191) NULL,
    `default_domain` BOOLEAN NOT NULL DEFAULT false,
    `active` BOOLEAN NOT NULL DEFAULT false,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `dids` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensip_dbaliases_id` INTEGER NULL,
    `opensip_alias_username` VARCHAR(191) NOT NULL DEFAULT '',
    `opensip_alias_domain` VARCHAR(191) NOT NULL DEFAULT '',
    `opensip_username` VARCHAR(191) NOT NULL DEFAULT '',
    `opensip_domain` VARCHAR(191) NOT NULL DEFAULT '',
    `opensip_domain_id` INTEGER NULL,
    `subscription_value` DOUBLE NOT NULL DEFAULT 0,
    `client_id` INTEGER NULL,
    `proxy_id` INTEGER NULL,
    `domain_id` INTEGER NOT NULL,
    `cnl` VARCHAR(191) NOT NULL DEFAULT '',
    `code_area` VARCHAR(191) NOT NULL DEFAULT '',
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `dids_opensip_dbaliases_id_key`(`opensip_dbaliases_id`),
    UNIQUE INDEX `dids_opensip_alias_username_key`(`opensip_alias_username`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `carrier` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensip_carrier_id` INTEGER NULL,
    `opensip_carrierid` VARCHAR(191) NOT NULL,
    `opensip_gwlist` VARCHAR(191) NOT NULL,
    `opensip_flags` INTEGER NOT NULL DEFAULT 0,
    `opensip_sort_alg` CHAR(1) NOT NULL DEFAULT 'N',
    `opensip_state` INTEGER NOT NULL DEFAULT 0,
    `opensip_attrs` VARCHAR(255) NULL,
    `opensip_description` VARCHAR(128) NULL,
    `charging_on_connection` BOOLEAN NOT NULL DEFAULT false,
    `rounding` VARCHAR(32) NOT NULL DEFAULT '',
    `purchase_connection_fee` VARCHAR(255) NOT NULL DEFAULT '',
    `purchase_cadence` INTEGER NOT NULL DEFAULT 0,
    `initial_delay` INTEGER NOT NULL DEFAULT 0,
    `disposal_fee` VARCHAR(255) NOT NULL DEFAULT '',
    `connection_value` VARCHAR(255) NOT NULL DEFAULT '',
    `quota_limit` VARCHAR(255) NOT NULL DEFAULT '',
    `call_0800` VARCHAR(12) NOT NULL DEFAULT '',
    `call_0300` VARCHAR(12) NOT NULL DEFAULT '',
    `call_0500` VARCHAR(12) NOT NULL DEFAULT '',
    `call_special_number` VARCHAR(12) NOT NULL DEFAULT '',
    `call_emergency_numbers` VARCHAR(12) NOT NULL DEFAULT '',
    `call_local_numbers` VARCHAR(12) NOT NULL DEFAULT '',
    `call_ldn_numbers` VARCHAR(12) NOT NULL DEFAULT '',
    `call_local_mobile_numbers` VARCHAR(12) NOT NULL DEFAULT '',
    `call_mobile_numbers_ldn_vc2` VARCHAR(12) NOT NULL DEFAULT '',
    `call_mobile_numbers_ldn_vc3` VARCHAR(12) NOT NULL DEFAULT '',
    `call_local_numbers_reduced_value` VARCHAR(12) NOT NULL DEFAULT '',
    `call_international_numbers` VARCHAR(12) NOT NULL DEFAULT '',
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `carrier_opensip_carrierid_key`(`opensip_carrierid`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `rules` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensip_ruleid` INTEGER NULL,
    `opensip_groupid` VARCHAR(191) NOT NULL,
    `opensip_prefix` VARCHAR(191) NOT NULL,
    `opensip_timerec` VARCHAR(191) NULL,
    `opensip_priority` INTEGER NOT NULL DEFAULT 0,
    `opensip_routeid` VARCHAR(191) NULL,
    `opensip_gwlist` VARCHAR(191) NULL,
    `opensip_sort_alg` VARCHAR(191) NOT NULL DEFAULT 'N',
    `opensip_sort_profile` INTEGER NULL,
    `opensip_attrs` VARCHAR(191) NULL,
    `opensip_description` VARCHAR(191) NULL,
    `reference` VARCHAR(191) NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `rules_reference_key`(`reference`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `gateways` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensip_gateway_id` INTEGER NOT NULL,
    `gwid` VARCHAR(191) NOT NULL,
    `type` INTEGER NOT NULL DEFAULT 0,
    `address` VARCHAR(191) NOT NULL,
    `strip` INTEGER NOT NULL DEFAULT 0,
    `pri_prefix` VARCHAR(191) NULL,
    `attrs` VARCHAR(191) NULL,
    `probe_mode` INTEGER NOT NULL DEFAULT 0,
    `state` INTEGER NOT NULL DEFAULT 0,
    `socket` VARCHAR(191) NULL,
    `description` VARCHAR(191) NULL,
    `carrier` INTEGER NOT NULL,
    `cli_support` BOOLEAN NOT NULL DEFAULT false,
    `type_cli` VARCHAR(191) NOT NULL DEFAULT 'None',
    `cli_format` VARCHAR(191) NOT NULL DEFAULT 'None',
    `caller_id` VARCHAR(191) NULL,
    `dial_plan_group` INTEGER NOT NULL DEFAULT 0,
    `max_calls_outbound` INTEGER NOT NULL DEFAULT 3,
    `max_calls_inbound` INTEGER NOT NULL DEFAULT 3,
    `timeout` INTEGER NOT NULL DEFAULT 0,
    `max_attempts_per_second` INTEGER NOT NULL DEFAULT 0,
    `monitoration` BOOLEAN NOT NULL DEFAULT true,
    `rate_id` VARCHAR(191) NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `transshipment_code` VARCHAR(191) NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `gateways_gwid_key`(`gwid`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `logins` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `domain` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `client_id` INTEGER NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `logins_username_key`(`username`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `dial-plan` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `dialplangroup_id` INTEGER NOT NULL,
    `opensip_dialplan_id` INTEGER NOT NULL,
    `opensip_dpid` INTEGER NOT NULL,
    `opensip_pr` INTEGER NOT NULL,
    `opensip_matchOp` INTEGER NOT NULL,
    `opensip_matchExp` VARCHAR(191) NOT NULL,
    `opensip_matchFlags` INTEGER NOT NULL,
    `opensip_substExp` VARCHAR(191) NULL,
    `opensip_replExp` VARCHAR(191) NULL,
    `opensip_timeRec` VARCHAR(191) NULL,
    `opensip_disable` INTEGER NOT NULL DEFAULT 0,
    `opensip_attrs` VARCHAR(191) NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `dial-plan_opensip_dialplan_id_key`(`opensip_dialplan_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `dial-plan-group` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `domain` VARCHAR(191) NOT NULL,
    `dial_plan_group` INTEGER NOT NULL,
    `rule_id` INTEGER NOT NULL,
    `dial_id` INTEGER NOT NULL,
    `priority` INTEGER NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `proxy` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensips_proxy_id` INTEGER NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `protocol` VARCHAR(191) NOT NULL,
    `ip` VARCHAR(191) NOT NULL,
    `port` VARCHAR(191) NOT NULL,
    `type` VARCHAR(191) NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `proxy_opensips_proxy_id_key`(`opensips_proxy_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `cdr` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `status` VARCHAR(191) NOT NULL,
    `direction` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `gateway_id` INTEGER NOT NULL,
    `proxy_id` INTEGER NOT NULL,
    `client_id` INTEGER NOT NULL,
    `did_id` INTEGER NOT NULL,
    `callId` VARCHAR(191) NOT NULL,
    `proxy_str` VARCHAR(191) NOT NULL,
    `gateway_str` VARCHAR(191) NOT NULL,
    `did_str` VARCHAR(191) NOT NULL,
    `from` VARCHAR(191) NOT NULL,
    `to` VARCHAR(191) NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `cdr_callId_key`(`callId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `cdr-metrics` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `cdr_id` INTEGER NOT NULL,
    `gateway_id` INTEGER NOT NULL,
    `proxy_id` INTEGER NOT NULL,
    `client_id` INTEGER NOT NULL,
    `did_id` INTEGER NOT NULL,
    `callId` VARCHAR(191) NOT NULL,
    `event` VARCHAR(191) NOT NULL,
    `direction` VARCHAR(191) NOT NULL,
    `code` VARCHAR(191) NOT NULL,
    `proxy_str` VARCHAR(191) NOT NULL,
    `gateway_str` VARCHAR(191) NOT NULL,
    `did_str` VARCHAR(191) NOT NULL,
    `from` VARCHAR(191) NOT NULL,
    `to` VARCHAR(191) NOT NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_modified` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `AuditLogVSR` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `action` VARCHAR(191) NOT NULL,
    `table_name` VARCHAR(191) NOT NULL,
    `record_id` INTEGER NOT NULL,
    `change_fields` TEXT NULL,
    `user_id` INTEGER NOT NULL,
    `ip_address` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `AuditLogOpensips` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `action` VARCHAR(191) NOT NULL,
    `table_name` VARCHAR(191) NOT NULL,
    `record_id` INTEGER NOT NULL,
    `change_fields` TEXT NULL,
    `user_id` INTEGER NOT NULL,
    `ip_address` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `address` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `opensips_address_id` INTEGER NOT NULL,
    `client_id` INTEGER NOT NULL,
    `domain_id` INTEGER NOT NULL,
    `title` VARCHAR(191) NOT NULL,
    `grp` INTEGER NOT NULL DEFAULT 0,
    `ip` VARCHAR(50) NOT NULL,
    `mask` CHAR(3) NOT NULL,
    `port` INTEGER NOT NULL DEFAULT 0,
    `proto` VARCHAR(4) NOT NULL DEFAULT 'any',
    `pattern` VARCHAR(64) NULL,
    `context_info` VARCHAR(32) NULL,
    `arquived` BOOLEAN NOT NULL DEFAULT false,

    UNIQUE INDEX `address_opensips_address_id_key`(`opensips_address_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `did_authentications` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `type` VARCHAR(191) NOT NULL DEFAULT 'login',
    `did_id` INTEGER NOT NULL,
    `login_id` INTEGER NULL,
    `address_by_ip_id` INTEGER NULL,

    UNIQUE INDEX `did_authentications_did_id_key`(`did_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `profiles` ADD CONSTRAINT `profiles_dialPlanGroup_id_fkey` FOREIGN KEY (`dialPlanGroup_id`) REFERENCES `dial-plan-group`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `tariffs` ADD CONSTRAINT `tariffs_domain_id_fkey` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `tariffs` ADD CONSTRAINT `tariffs_rule_id_fkey` FOREIGN KEY (`rule_id`) REFERENCES `rules`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_profile_id_fkey` FOREIGN KEY (`profile_id`) REFERENCES `profiles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `clients` ADD CONSTRAINT `clients_tariff_id_fkey` FOREIGN KEY (`tariff_id`) REFERENCES `tariffs`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `dids` ADD CONSTRAINT `dids_client_id_fkey` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `dids` ADD CONSTRAINT `dids_proxy_id_fkey` FOREIGN KEY (`proxy_id`) REFERENCES `proxy`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `gateways` ADD CONSTRAINT `gateways_carrier_fkey` FOREIGN KEY (`carrier`) REFERENCES `carrier`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `logins` ADD CONSTRAINT `logins_client_id_fkey` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `dial-plan` ADD CONSTRAINT `dial-plan_dialplangroup_id_fkey` FOREIGN KEY (`dialplangroup_id`) REFERENCES `dial-plan-group`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `dial-plan-group` ADD CONSTRAINT `dial-plan-group_rule_id_fkey` FOREIGN KEY (`rule_id`) REFERENCES `rules`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_gateway_id_fkey` FOREIGN KEY (`gateway_id`) REFERENCES `gateways`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_proxy_id_fkey` FOREIGN KEY (`proxy_id`) REFERENCES `proxy`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_client_id_fkey` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr` ADD CONSTRAINT `cdr_did_id_fkey` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_gateway_id_fkey` FOREIGN KEY (`gateway_id`) REFERENCES `gateways`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_proxy_id_fkey` FOREIGN KEY (`proxy_id`) REFERENCES `proxy`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_client_id_fkey` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_did_id_fkey` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `cdr-metrics` ADD CONSTRAINT `cdr-metrics_cdr_id_fkey` FOREIGN KEY (`cdr_id`) REFERENCES `cdr`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `AuditLogVSR` ADD CONSTRAINT `AuditLogVSR_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `AuditLogOpensips` ADD CONSTRAINT `AuditLogOpensips_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `address` ADD CONSTRAINT `address_client_id_fkey` FOREIGN KEY (`client_id`) REFERENCES `clients`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `address` ADD CONSTRAINT `address_domain_id_fkey` FOREIGN KEY (`domain_id`) REFERENCES `domain`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_did_id_fkey` FOREIGN KEY (`did_id`) REFERENCES `dids`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_login_id_fkey` FOREIGN KEY (`login_id`) REFERENCES `logins`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `did_authentications` ADD CONSTRAINT `did_authentications_address_by_ip_id_fkey` FOREIGN KEY (`address_by_ip_id`) REFERENCES `address`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
