-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Jun 25, 2024 at 09:27 PM
-- Server version: 10.6.18-MariaDB
-- PHP Version: 8.1.28

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

--
-- Database: `mtrnau66_helpdesk`
--
CREATE DATABASE IF NOT EXISTS `mtrnau66_helpdesk` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `mtrnau66_helpdesk`;

-- --------------------------------------------------------

--
-- Table structure for table `comments`
--

CREATE TABLE `comments` (
                            `cid` int(11) NOT NULL,
                            `ticket_id` int(11) NOT NULL,
                            `uid` int(11) DEFAULT NULL,
                            `comment` longtext NOT NULL,
                            `is_public` tinyint(4) NOT NULL DEFAULT 1,
                            `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
                            `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `domains`
--

CREATE TABLE `domains` (
                           `did` int(11) NOT NULL,
                           `tld` varchar(255) NOT NULL,
                           `oid` int(11) NOT NULL,
                           `isPrimary` tinyint(1) NOT NULL DEFAULT 1,
                           `isAlias` tinyint(1) DEFAULT 0,
                           `alias_parent_id` int(11) DEFAULT NULL,
                           `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
                           `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
                           `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `organisations`
--

CREATE TABLE `organisations` (
                                 `oid` int(11) NOT NULL,
                                 `organisation_name` varchar(255) NOT NULL,
                                 `microsoft_tenant_id` longtext DEFAULT NULL,
                                 `microsoft_client_id` longtext DEFAULT NULL,
                                 `microsoft_client_secret` longtext DEFAULT NULL,
                                 `microsoft_app_id` longtext DEFAULT NULL,
                                 `status` enum('ACTIVE','INACTIVE','DELETED','SUSPENDED') NOT NULL DEFAULT 'ACTIVE',
                                 `total_billed_time` time DEFAULT '00:00:00',
                                 `monthly_billed_time` time DEFAULT '00:00:00',
                                 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
                                 `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
                                 `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `processed_emails`
--

CREATE TABLE `processed_emails` (
                                    `id` int(11) NOT NULL,
                                    `email_id` varchar(255) NOT NULL,
                                    `processed_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `tickets`
--

CREATE TABLE `tickets` (
                           `tid` int(11) NOT NULL,
                           `requester_id` int(11) NOT NULL,
                           `assigned_uid` int(11) DEFAULT NULL,
                           `subject` varchar(255) NOT NULL,
                           `description` longtext NOT NULL,
                           `state` enum('NEW','OPEN','AWAITING_CUSTOMER','AWAITING_THIRD_PARTY','ON_HOLD','SOLVED','CLOSED','DELETED','STALE') NOT NULL DEFAULT 'NEW',
                           `view_password` varchar(255) DEFAULT NULL,
                           `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
                           `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
                           `deleted_at` timestamp NULL DEFAULT NULL,
                           `has_attachments` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `ticket_watchers`
--

CREATE TABLE `ticket_watchers` (
                                   `id` int(11) NOT NULL,
                                   `ticket_id` int(11) DEFAULT NULL,
                                   `user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `tracking`
--

CREATE TABLE `tracking` (
                            `tracking_id` int(11) NOT NULL,
                            `session_id` varchar(255) DEFAULT NULL,
                            `user_agent` varchar(255) DEFAULT NULL,
                            `uri` longtext NOT NULL,
                            `is_logged_in` tinyint(1) NOT NULL DEFAULT 0,
                            `user_data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`user_data`)),
                            `ip` varchar(255) DEFAULT NULL,
                            `deleted_at` timestamp NULL DEFAULT NULL,
                            `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
                            `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
                         `uid` int(11) NOT NULL,
                         `first_name` varchar(255) DEFAULT NULL,
                         `last_name` varchar(255) DEFAULT NULL,
                         `email` varchar(255) NOT NULL,
                         `password_hash` longtext NOT NULL,
                         `status` enum('ACTIVE','INACTIVE','SUSPENDED','DELETED','SPAM') NOT NULL DEFAULT 'ACTIVE',
                         `role` enum('SUPER_ADMIN','ADMIN','AGENT','USER') NOT NULL DEFAULT 'USER',
                         `oid` int(11) DEFAULT NULL,
                         `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
                         `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
                         `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `comments`
--
ALTER TABLE `comments`
    ADD PRIMARY KEY (`cid`),
    ADD KEY `TICKET_ID` (`ticket_id`),
    ADD KEY `USER_ID` (`uid`);

--
-- Indexes for table `domains`
--
ALTER TABLE `domains`
    ADD PRIMARY KEY (`did`),
    ADD UNIQUE KEY `tld` (`tld`),
    ADD KEY `ALIAS_PARENT_DID` (`alias_parent_id`),
    ADD KEY `ORG_ID_DID` (`oid`);

--
-- Indexes for table `organisations`
--
ALTER TABLE `organisations`
    ADD PRIMARY KEY (`oid`),
    ADD UNIQUE KEY `organisation_name` (`organisation_name`);

--
-- Indexes for table `processed_emails`
--
ALTER TABLE `processed_emails`
    ADD PRIMARY KEY (`id`),
    ADD UNIQUE KEY `email_id` (`email_id`);

--
-- Indexes for table `tickets`
--
ALTER TABLE `tickets`
    ADD PRIMARY KEY (`tid`),
    ADD KEY `UID_ON_REQUESTER` (`requester_id`),
    ADD KEY `UID_ON_ASSIGNEE` (`assigned_uid`);

--
-- Indexes for table `ticket_watchers`
--
ALTER TABLE `ticket_watchers`
    ADD PRIMARY KEY (`id`),
    ADD UNIQUE KEY `ticket_id` (`ticket_id`,`user_id`),
    ADD KEY `user_id` (`user_id`);

--
-- Indexes for table `tracking`
--
ALTER TABLE `tracking`
    ADD PRIMARY KEY (`tracking_id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
    ADD PRIMARY KEY (`uid`),
    ADD UNIQUE KEY `uid` (`uid`,`first_name`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `comments`
--
ALTER TABLE `comments`
    MODIFY `cid` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `domains`
--
ALTER TABLE `domains`
    MODIFY `did` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `organisations`
--
ALTER TABLE `organisations`
    MODIFY `oid` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `processed_emails`
--
ALTER TABLE `processed_emails`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `tickets`
--
ALTER TABLE `tickets`
    MODIFY `tid` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `ticket_watchers`
--
ALTER TABLE `ticket_watchers`
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `tracking`
--
ALTER TABLE `tracking`
    MODIFY `tracking_id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
    MODIFY `uid` int(11) NOT NULL AUTO_INCREMENT;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `comments`
--
ALTER TABLE `comments`
    ADD CONSTRAINT `TICKET_ID` FOREIGN KEY (`ticket_id`) REFERENCES `tickets` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE,
    ADD CONSTRAINT `USER_ID` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`);

--
-- Constraints for table `domains`
--
ALTER TABLE `domains`
    ADD CONSTRAINT `ALIAS_PARENT_DID` FOREIGN KEY (`alias_parent_id`) REFERENCES `domains` (`did`) ON DELETE CASCADE ON UPDATE CASCADE,
    ADD CONSTRAINT `ORG_ID_DID` FOREIGN KEY (`oid`) REFERENCES `organisations` (`oid`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `tickets`
--
ALTER TABLE `tickets`
    ADD CONSTRAINT `UID_ON_ASSIGNEE` FOREIGN KEY (`assigned_uid`) REFERENCES `users` (`uid`),
    ADD CONSTRAINT `UID_ON_REQUESTER` FOREIGN KEY (`requester_id`) REFERENCES `users` (`uid`);
COMMIT;
