-- --------------------------------------------------------
-- Host:                         iceberg-gaming.com
-- Server version:               10.6.5-MariaDB-log - mariadb.org binary distribution
-- Server OS:                    Win64
-- HeidiSQL Version:             12.13.0.7147
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


-- Dumping database structure for milsim_website_development
CREATE DATABASE IF NOT EXISTS `milsim_website_development` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE `milsim_website_development`;

-- Dumping structure for table milsim_website_development.application_comments
CREATE TABLE IF NOT EXISTS `application_comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `post_content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `application_id` int(11) NOT NULL,
  `poster_id` int(11) NOT NULL,
  `post_time` timestamp NOT NULL DEFAULT current_timestamp(),
  `last_modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `admin_only` tinyint(1) DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `poster_id` (`poster_id`),
  KEY `application_id` (`application_id`),
  CONSTRAINT `application_comments_ibfk_1` FOREIGN KEY (`application_id`) REFERENCES `applications` (`id`),
  CONSTRAINT `application_comments_ibfk_2` FOREIGN KEY (`poster_id`) REFERENCES `members` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.applications
CREATE TABLE IF NOT EXISTS `applications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `app_version` int(11) NOT NULL,
  `app_data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`app_data`)),
  `submitted_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT NULL,
  `approved_at` datetime DEFAULT NULL,
  `denied_at` datetime DEFAULT NULL,
  `app_status` varchar(20) GENERATED ALWAYS AS (case when `approved_at` is not null then 'Accepted' when `denied_at` is not null then 'Denied' else 'Pending' end) STORED,
  `decision_at` datetime GENERATED ALWAYS AS (coalesce(`approved_at`,`denied_at`)) STORED,
  `approved_by` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `member_id` (`member_id`),
  KEY `FK_applications_members` (`approved_by`),
  CONSTRAINT `FK_applications_members` FOREIGN KEY (`approved_by`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_app_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `chk_json_valid` CHECK (json_valid(`app_data`)),
  CONSTRAINT `chk_app_version` CHECK (`app_version` >= 1),
  CONSTRAINT `chk_exclusive_decision` CHECK (`approved_at` is null or `denied_at` is null)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.arma_maps
CREATE TABLE IF NOT EXISTS `arma_maps` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `map` varchar(50) DEFAULT NULL,
  `name` text DEFAULT NULL,
  `last_used` datetime DEFAULT NULL,
  `deleted` tinyint(4) DEFAULT 0,
  `workshop_id` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `map` (`map`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Contains a list of Arma3 Maps';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.awards
CREATE TABLE IF NOT EXISTS `awards` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` longtext DEFAULT NULL,
  `short_name` longtext DEFAULT NULL,
  `description` longtext DEFAULT NULL,
  `type` longtext DEFAULT NULL,
  `footprint` longtext DEFAULT NULL,
  `created_at` datetime(3) NOT NULL,
  `updated_at` datetime(3) NOT NULL,
  `image_url` longtext DEFAULT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  `deleted_at` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_awards_deleted` (`deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8mb4 COMMENT='Contains a list of Awards for the unit.';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.calendar_events
CREATE TABLE IF NOT EXISTS `calendar_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `start` datetime NOT NULL,
  `end` datetime NOT NULL,
  `location` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `color` char(7) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '#000000',
  `description` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `creator` int(11) DEFAULT NULL,
  `cancelled` tinyint(1) DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `full_day` tinyint(4) DEFAULT NULL,
  `guilded_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `guilded_channel_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_creator` (`creator`),
  CONSTRAINT `FK_calendar_events_members` FOREIGN KEY (`creator`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3346 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.calendar_events_signups
CREATE TABLE IF NOT EXISTS `calendar_events_signups` (
  `member_id` int(11) NOT NULL,
  `event_id` int(11) NOT NULL,
  `status` enum('not_attending','attending','maybe') COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `guilded_member_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `guilded_event_id` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`member_id`,`event_id`),
  UNIQUE KEY `guilded_member_id_guilded_event_id` (`guilded_member_id`,`guilded_event_id`),
  KEY `fk_signup_event` (`event_id`),
  CONSTRAINT `fk_signup_event` FOREIGN KEY (`event_id`) REFERENCES `calendar_events` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_signup_member` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.course_attendee_roles
CREATE TABLE IF NOT EXISTS `course_attendee_roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `description` text DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `type` (`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COMMENT='Changed from course_attendee_type to event_attendee_type';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.course_attendees
CREATE TABLE IF NOT EXISTS `course_attendees` (
  `passed_bookwork` tinyint(1) NOT NULL DEFAULT 0,
  `passed_qual` tinyint(1) NOT NULL DEFAULT 0,
  `attendee_id` int(11) NOT NULL,
  `course_event_id` int(11) NOT NULL,
  `attendee_role_id` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `remarks` text DEFAULT NULL,
  `expired` tinyint(4) NOT NULL DEFAULT 0,
  `expired_reason` varchar(50) NOT NULL DEFAULT '0',
  PRIMARY KEY (`attendee_id`,`course_event_id`) USING BTREE,
  KEY `courseInstanceId` (`course_event_id`) USING BTREE,
  KEY `fk_CourseInstancesMembers_CoureseAttendeeType_id` (`attendee_role_id`) USING BTREE,
  CONSTRAINT `fk_course_event_member_coures_attendee_type_id` FOREIGN KEY (`attendee_role_id`) REFERENCES `course_attendee_roles` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_course_event_members_coures_event_id` FOREIGN KEY (`course_event_id`) REFERENCES `course_events` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_course_event_members_members_id` FOREIGN KEY (`attendee_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.course_category
CREATE TABLE IF NOT EXISTS `course_category` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.course_events
CREATE TABLE IF NOT EXISTS `course_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `course_id` int(11) DEFAULT NULL,
  `event_type` int(11) DEFAULT NULL,
  `event_date` datetime NOT NULL,
  `guilded_event_id` varchar(50) DEFAULT NULL,
  `guilded_report_id` varchar(50) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT 0,
  `report_url` varchar(2048) DEFAULT NULL,
  `remarks` text DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `hasBookwork` tinyint(4) DEFAULT NULL,
  `hasQual` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_course_events_event_type_id` (`event_type`) USING BTREE,
  KEY `courseId` (`course_id`) USING BTREE,
  KEY `fk_course_events_created_by` (`created_by`),
  CONSTRAINT `fk_coures_events_course_id` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_course_events_created_by` FOREIGN KEY (`created_by`) REFERENCES `members` (`id`),
  CONSTRAINT `fk_course_events_event_type_id` FOREIGN KEY (`event_type`) REFERENCES `event_types` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.course_qualified_trainers
CREATE TABLE IF NOT EXISTS `course_qualified_trainers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) DEFAULT NULL,
  `course_id` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `qualified` tinyint(4) DEFAULT NULL,
  `instance_qualified_id` int(11) DEFAULT NULL,
  `deleted` tinyint(4) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `memberId_courseId` (`member_id`,`course_id`) USING BTREE,
  KEY `fk_coures_qualified_trainers_coures_id` (`course_id`) USING BTREE,
  KEY `fk_CourseTrainers_CourseInstance_id` (`instance_qualified_id`) USING BTREE,
  CONSTRAINT `fk_coures_qualified_trainers_coures_events_id` FOREIGN KEY (`instance_qualified_id`) REFERENCES `course_events` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_coures_qualified_trainers_coures_id` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_coures_qualified_trainers_mebers_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Contains a linked group of qualified trainers';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.courses
CREATE TABLE IF NOT EXISTS `courses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `short_name` varchar(10) NOT NULL,
  `category` varchar(100) NOT NULL,
  `description` varchar(1000) DEFAULT NULL,
  `image_url` varchar(255) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT 0,
  `prereq_id` int(11) DEFAULT NULL,
  `hasBookwork` tinyint(1) NOT NULL DEFAULT 0,
  `hasQual` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  UNIQUE KEY `shortName` (`short_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.courses_sme
CREATE TABLE IF NOT EXISTS `courses_sme` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `course_id` int(11) NOT NULL DEFAULT 0,
  `member_id` int(11) NOT NULL DEFAULT 0,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `fk_course_sme_course_id` (`course_id`),
  KEY `fk_course_sme_member_id` (`member_id`),
  CONSTRAINT `fk_course_sme_course_id` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_course_sme_member_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.event_types
CREATE TABLE IF NOT EXISTS `event_types` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_type` varchar(100) DEFAULT NULL,
  `event_category` varchar(100) DEFAULT NULL,
  `deleted` tinyint(4) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.guilded_events
CREATE TABLE IF NOT EXISTS `guilded_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `channel_id` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `event_id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `event_type` int(11) DEFAULT NULL,
  `event_date` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` int(11) DEFAULT NULL,
  `url` varchar(2048) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `calendar_id_event_id` (`channel_id`,`event_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.leave_of_absences
CREATE TABLE IF NOT EXISTS `leave_of_absences` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) DEFAULT NULL,
  `filed_date` datetime NOT NULL COMMENT 'Date that the LOA was filed',
  `start_date` datetime NOT NULL COMMENT 'Date that the LOA should Start',
  `end_date` datetime NOT NULL COMMENT 'Date that the LOA should End',
  `extended_till` datetime DEFAULT NULL,
  `type_id` int(11) DEFAULT NULL,
  `reason` text DEFAULT NULL COMMENT 'Reason a person has put in the LOA',
  `created_by_id` int(11) DEFAULT NULL,
  `deleted` tinyint(4) NOT NULL DEFAULT 0,
  `expired` tinyint(4) DEFAULT NULL COMMENT 'If a LOA is Expired or not',
  `closed_by` int(11) DEFAULT NULL COMMENT 'Who Closed the LOA if it was manually Closed',
  `ended_at` date DEFAULT NULL,
  `closed` tinyint(4) DEFAULT NULL COMMENT 'If a LOA was closed early or ',
  `created_at` datetime NOT NULL DEFAULT current_timestamp() COMMENT 'Database Record Creation Date',
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'Database Record Modified Date',
  PRIMARY KEY (`id`),
  KEY `fk_leave_of_absesnse_members_id` (`member_id`) USING BTREE,
  KEY `FK_leave_of_absences_leave_of_absences_types` (`type_id`),
  KEY `c` (`created_by_id`),
  CONSTRAINT `FK_leave_of_absences_leave_of_absences_types` FOREIGN KEY (`type_id`) REFERENCES `leave_of_absences_types` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `c` FOREIGN KEY (`created_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_leave_of_absesnse_members_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.leave_of_absences_types
CREATE TABLE IF NOT EXISTS `leave_of_absences_types` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `max_length_days` int(11) NOT NULL DEFAULT 0,
  `extendable` tinyint(4) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members
CREATE TABLE IF NOT EXISTS `members` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `timezone` varchar(5) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `website` varchar(240) DEFAULT NULL,
  `guilded_id` varchar(10) DEFAULT NULL,
  `steam_id_64` varchar(17) DEFAULT NULL,
  `teamspeak_uid` varchar(32) DEFAULT NULL,
  `steam_profile_name` varchar(32) DEFAULT NULL,
  `discord_id` varchar(20) DEFAULT NULL,
  `discord_username` varchar(32) DEFAULT NULL,
  `aliases` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`aliases`)),
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT NULL,
  `remarks` text DEFAULT NULL,
  `authentik_sub` varchar(255) DEFAULT NULL,
  `authentik_issuer` varchar(255) DEFAULT NULL,
  `state` enum('guest','applicant','member','retired','banned','denied') NOT NULL DEFAULT 'member',
  `displayName` varchar(50) DEFAULT NULL,
  `last_activity` datetime DEFAULT NULL,
  `last_login` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  UNIQUE KEY `steamId64` (`steam_id_64`) USING BTREE,
  UNIQUE KEY `discordId` (`discord_id`) USING BTREE,
  UNIQUE KEY `guilded_id` (`guilded_id`),
  UNIQUE KEY `uk_authentik_identity` (`authentik_issuer`,`authentik_sub`)
) ENGINE=InnoDB AUTO_INCREMENT=3349 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_awards
CREATE TABLE IF NOT EXISTS `members_awards` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) DEFAULT NULL,
  `awards_id` int(11) DEFAULT NULL,
  `event_date` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `fk_members_awards_membres_id` (`member_id`),
  KEY `fk_members_awards_awards_id` (`awards_id`),
  CONSTRAINT `fk_members_awards_awards_id` FOREIGN KEY (`awards_id`) REFERENCES `awards` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_members_awards_membres_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_qualifications
CREATE TABLE IF NOT EXISTS `members_qualifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) DEFAULT 0,
  `qualification_id` int(11) DEFAULT 0,
  `event_date` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_members_qualifications_member_id` (`member_id`),
  KEY `fk_members_qualifications_qualifications_id` (`qualification_id`),
  CONSTRAINT `fk_members_qualifications_member_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_members_qualifications_qualifications_id` FOREIGN KEY (`qualification_id`) REFERENCES `qualifications` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_ranks
CREATE TABLE IF NOT EXISTS `members_ranks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `rank_id` int(11) NOT NULL,
  `authorized_by_id` int(11) DEFAULT NULL,
  `created_by_id` int(11) DEFAULT NULL,
  `reason` varchar(50) DEFAULT NULL,
  `start_date` datetime NOT NULL DEFAULT current_timestamp(),
  `end_date` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `fk_members_ranks_members_id` (`member_id`),
  KEY `fk_members_ranks_rank_id` (`rank_id`),
  KEY `FK_members_ranks_members_2` (`authorized_by_id`),
  KEY `FK_members_ranks_members_3` (`created_by_id`),
  CONSTRAINT `FK_members_ranks_members` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_ranks_members_2` FOREIGN KEY (`authorized_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_ranks_members_3` FOREIGN KEY (`created_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_members_ranks_rank_id` FOREIGN KEY (`rank_id`) REFERENCES `ranks` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_roles
CREATE TABLE IF NOT EXISTS `members_roles` (
  `member_id` int(11) NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`member_id`,`role_id`),
  KEY `role_id` (`role_id`),
  CONSTRAINT `members_roles_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON DELETE CASCADE,
  CONSTRAINT `members_roles_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_statuses
CREATE TABLE IF NOT EXISTS `members_statuses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `status_id` int(11) NOT NULL,
  `authorized_by_id` int(11) DEFAULT NULL,
  `created_by_id` int(11) DEFAULT NULL,
  `reason` varchar(50) DEFAULT NULL,
  `start_date` datetime NOT NULL DEFAULT current_timestamp(),
  `end_date` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `fk_members_statuses_status_id` (`status_id`),
  KEY `fk_members_statuses_member_id` (`member_id`),
  KEY `FK_members_statuses_ranger_unit_tracker.members` (`authorized_by_id`),
  KEY `FK_members_statuses_ranger_unit_tracker.members_2` (`created_by_id`),
  CONSTRAINT `FK_members_statuses_members` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_statuses_ranger_unit_tracker.members` FOREIGN KEY (`authorized_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_statuses_ranger_unit_tracker.members_2` FOREIGN KEY (`created_by_id`) REFERENCES `members` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_members_statuses_status_id` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.members_unit
CREATE TABLE IF NOT EXISTS `members_unit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `member_id` int(11) NOT NULL,
  `unit_id` int(11) NOT NULL,
  `authorized_by_id` int(11) NOT NULL,
  `created_by_id` int(11) NOT NULL,
  `reason` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `start_date` datetime NOT NULL DEFAULT current_timestamp(),
  `end_date` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `FK_members_unit_units` (`unit_id`),
  KEY `FK_members_unit_members` (`member_id`),
  KEY `FK_members_unit_members_2` (`authorized_by_id`),
  KEY `FK_members_unit_members_3` (`created_by_id`),
  CONSTRAINT `FK_members_unit_members` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_unit_members_2` FOREIGN KEY (`authorized_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_unit_members_3` FOREIGN KEY (`created_by_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_members_unit_units` FOREIGN KEY (`unit_id`) REFERENCES `units` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=96 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.mission_attendee_roles
CREATE TABLE IF NOT EXISTS `mission_attendee_roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `short_name` varchar(50) DEFAULT NULL,
  `description` text DEFAULT NULL,
  `deleted` tinyint(4) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `role_name` (`name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.mission_event_attendees
CREATE TABLE IF NOT EXISTS `mission_event_attendees` (
  `id` int(11) DEFAULT NULL,
  `member_id` int(11) NOT NULL,
  `event_id` int(11) NOT NULL,
  `member_role_id` int(11) DEFAULT NULL,
  `event_type` int(11) NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`member_id`,`event_id`) USING BTREE,
  KEY `fk_mission_event_attendees_role_id` (`member_role_id`),
  KEY `fk_mission_event_events_id` (`event_id`),
  CONSTRAINT `fk_member_event_member_id` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_mission_event_attendees_role_id` FOREIGN KEY (`member_role_id`) REFERENCES `mission_attendee_roles` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_mission_event_events_id` FOREIGN KEY (`event_id`) REFERENCES `mission_events` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.mission_events
CREATE TABLE IF NOT EXISTS `mission_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_type_id` int(11) NOT NULL,
  `event_name` varchar(100) NOT NULL,
  `description` varchar(100) DEFAULT NULL,
  `mission_name` varchar(100) NOT NULL,
  `author_id` int(11) DEFAULT NULL,
  `map_id` int(11) DEFAULT NULL,
  `event_date` datetime NOT NULL,
  `guilded_event_id` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `event_name` (`event_name`),
  KEY `fk_events_author_member_id` (`author_id`) USING BTREE,
  KEY `fk_mission_event_type_id` (`event_type_id`),
  KEY `fk_mission_event_map_id` (`map_id`),
  CONSTRAINT `fk_events_author_id` FOREIGN KEY (`author_id`) REFERENCES `members` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_mission_event_map_id` FOREIGN KEY (`map_id`) REFERENCES `arma_maps` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `fk_mission_event_type_id` FOREIGN KEY (`event_type_id`) REFERENCES `event_types` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.qualifications
CREATE TABLE IF NOT EXISTS `qualifications` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `short_name` varchar(10) DEFAULT NULL,
  `description` text DEFAULT NULL,
  `type` varchar(100) DEFAULT NULL,
  `classification` varchar(100) DEFAULT NULL,
  `footprint` varchar(50) DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `image_url` varchar(250) DEFAULT NULL,
  `deleted` tinytext DEFAULT '0',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `name` (`name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Contains a list of Member Qualifications for the unit.';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.ranks
CREATE TABLE IF NOT EXISTS `ranks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` longtext DEFAULT NULL,
  `short_name` varchar(70) NOT NULL,
  `category` varchar(100) NOT NULL,
  `sort_id` int(11) NOT NULL DEFAULT 0,
  `image_url` varchar(240) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL,
  `updated_at` datetime(3) NOT NULL,
  `deleted` tinyint(1) NOT NULL DEFAULT 0,
  `deleted_at` datetime(3) DEFAULT NULL,
  `discord_role_id` varchar(50) DEFAULT NULL,
  `teamspeak_role_id` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `shortName` (`short_name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.roles
CREATE TABLE IF NOT EXISTS `roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
  `color` varchar(9) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '#000000',
  `description` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.site_config
CREATE TABLE IF NOT EXISTS `site_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `discord_guild_id` int(11) DEFAULT NULL,
  `application_unit_on_accept` int(11) DEFAULT NULL,
  `application_rank_on_accept` int(11) DEFAULT NULL,
  `application_status_on_accept` int(11) DEFAULT NULL,
  `loa_consecutive_gap_days` int(11) DEFAULT 0,
  `loa_consecutive_count` int(11) DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `FK_site_config_units` (`application_unit_on_accept`),
  KEY `FK_site_config_ranks` (`application_rank_on_accept`),
  KEY `FK_site_config_statuses` (`application_status_on_accept`),
  CONSTRAINT `FK_site_config_ranks` FOREIGN KEY (`application_rank_on_accept`) REFERENCES `ranks` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_site_config_statuses` FOREIGN KEY (`application_status_on_accept`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `FK_site_config_units` FOREIGN KEY (`application_unit_on_accept`) REFERENCES `units` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for procedure milsim_website_development.sp_accept_new_recruit_validation
DELIMITER //
//
DELIMITER ;

-- Dumping structure for procedure milsim_website_development.sp_GetCalendarEventSignups
DELIMITER //
//
DELIMITER ;

-- Dumping structure for procedure milsim_website_development.sp_manual_update_report
DELIMITER //
//
DELIMITER ;

-- Dumping structure for procedure milsim_website_development.sp_update_member_rank
DELIMITER //
//
DELIMITER ;

-- Dumping structure for procedure milsim_website_development.sp_update_member_status
DELIMITER //
//
DELIMITER ;

-- Dumping structure for procedure milsim_website_development.sp_update_member_unit
DELIMITER //
//
DELIMITER ;

-- Dumping structure for table milsim_website_development.status_change_requests
CREATE TABLE IF NOT EXISTS `status_change_requests` (
  `id` int(11) NOT NULL,
  `member_id` int(11) DEFAULT NULL,
  `current_status` int(11) DEFAULT NULL,
  `next_status` int(11) DEFAULT NULL,
  `next_rank` int(11) DEFAULT NULL,
  `approval_from` int(11) DEFAULT NULL,
  `approval_from_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `approval_to` int(11) DEFAULT NULL,
  `approval_to_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `request_date` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `member_id` (`member_id`),
  KEY `next_status` (`next_status`),
  KEY `next_rank` (`next_rank`),
  KEY `approval_from` (`approval_from`),
  KEY `approval_to` (`approval_to`),
  KEY `current_status` (`current_status`),
  CONSTRAINT `status_change_requests_ibfk_1` FOREIGN KEY (`member_id`) REFERENCES `members` (`id`),
  CONSTRAINT `status_change_requests_ibfk_2` FOREIGN KEY (`next_status`) REFERENCES `statuses` (`id`),
  CONSTRAINT `status_change_requests_ibfk_3` FOREIGN KEY (`next_rank`) REFERENCES `ranks` (`id`),
  CONSTRAINT `status_change_requests_ibfk_4` FOREIGN KEY (`approval_from`) REFERENCES `members` (`id`),
  CONSTRAINT `status_change_requests_ibfk_5` FOREIGN KEY (`approval_to`) REFERENCES `members` (`id`),
  CONSTRAINT `status_change_requests_ibfk_6` FOREIGN KEY (`current_status`) REFERENCES `statuses` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.statuses
CREATE TABLE IF NOT EXISTS `statuses` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.statuses_reasons
CREATE TABLE IF NOT EXISTS `statuses_reasons` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT current_timestamp(),
  `updated_at` datetime DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `deleted` tinyint(4) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Contains a list of Reasons that a Status Change occured. This helps determine Promotions, Demotions, Transfers, Joins, Leaves.';

-- Data exporting was unselected.

-- Dumping structure for table milsim_website_development.units
CREATE TABLE IF NOT EXISTS `units` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `type` enum('Company','Status','External') DEFAULT NULL,
  `is_internal` tinyint(4) NOT NULL,
  `description` text DEFAULT NULL,
  `active` tinyint(4) NOT NULL DEFAULT 1,
  `color` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;

-- Data exporting was unselected.

-- Dumping structure for view milsim_website_development.view_member_latest_info
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `view_member_latest_info` (
	`member_id` INT(11) NOT NULL,
	`member_name` VARCHAR(1) NOT NULL COLLATE 'utf8mb4_general_ci',
	`rank` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
	`rank_date` DATETIME NULL,
	`status` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci',
	`status_date` DATETIME NULL
);

-- Dumping structure for view milsim_website_development.view_member_rank_status_all
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `view_member_rank_status_all` (
	`member_id` INT(11) NOT NULL,
	`member_name` VARCHAR(1) NOT NULL COLLATE 'utf8mb4_general_ci',
	`rank` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
	`rank_date` DATETIME NULL,
	`status` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci',
	`status_date` DATETIME NULL
);

-- Dumping structure for view milsim_website_development.view_member_rank_unit_status_latest
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `view_member_rank_unit_status_latest` (
	`member_id` INT(11) NOT NULL,
	`member_name` VARCHAR(1) NOT NULL COLLATE 'utf8mb4_general_ci',
	`displayName` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci',
	`rank` LONGTEXT NULL COLLATE 'utf8mb4_general_ci',
	`rank_date` DATETIME NULL,
	`status` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci',
	`status_date` DATETIME NULL,
	`unit` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci',
	`unit_date` DATETIME NULL,
	`loa_until` DATE NULL
);

-- Dumping structure for view milsim_website_development.view_member_settings
-- Creating temporary table to overcome VIEW dependency errors
CREATE TABLE `view_member_settings` (
	`id` INT(11) NOT NULL,
	`displayName` VARCHAR(1) NULL COLLATE 'utf8mb4_general_ci'
);

-- Removing temporary table and create final VIEW structure
DROP TABLE IF EXISTS `view_member_latest_info`;
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `view_member_latest_info` AS select `m`.`id` AS `member_id`,`m`.`name` AS `member_name`,`r`.`name` AS `rank`,`mr`.`start_date` AS `rank_date`,`s`.`name` AS `status`,`ms`.`start_date` AS `status_date` from ((((`milsim_website_development`.`members` `m` left join (select `mr`.`id` AS `id`,`mr`.`member_id` AS `member_id`,`mr`.`rank_id` AS `rank_id`,`mr`.`start_date` AS `start_date`,`mr`.`created_at` AS `created_at`,`mr`.`updated_at` AS `updated_at` from ((`milsim_website_development`.`members_ranks` `mr` join (select `milsim_website_development`.`members_ranks`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_ranks`.`start_date`) AS `max_date` from `milsim_website_development`.`members_ranks` group by `milsim_website_development`.`members_ranks`.`member_id`) `lr` on(`lr`.`member_id` = `mr`.`member_id` and `lr`.`max_date` = `mr`.`start_date`)) join (select `milsim_website_development`.`members_ranks`.`member_id` AS `member_id`,`milsim_website_development`.`members_ranks`.`start_date` AS `start_date`,max(`milsim_website_development`.`members_ranks`.`id`) AS `max_id` from `milsim_website_development`.`members_ranks` group by `milsim_website_development`.`members_ranks`.`member_id`,`milsim_website_development`.`members_ranks`.`start_date`) `lr2` on(`lr2`.`member_id` = `mr`.`member_id` and `lr2`.`start_date` = `mr`.`start_date` and `lr2`.`max_id` = `mr`.`id`))) `mr` on(`mr`.`member_id` = `m`.`id`)) left join `milsim_website_development`.`ranks` `r` on(`r`.`id` = `mr`.`rank_id`)) left join (select `ms`.`id` AS `id`,`ms`.`member_id` AS `member_id`,`ms`.`status_id` AS `status_id`,`ms`.`start_date` AS `start_date`,`ms`.`created_at` AS `created_at`,`ms`.`updated_at` AS `updated_at` from ((`milsim_website_development`.`members_statuses` `ms` join (select `milsim_website_development`.`members_statuses`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_statuses`.`start_date`) AS `max_date` from `milsim_website_development`.`members_statuses` group by `milsim_website_development`.`members_statuses`.`member_id`) `ls` on(`ls`.`member_id` = `ms`.`member_id` and `ls`.`max_date` = `ms`.`start_date`)) join (select `milsim_website_development`.`members_statuses`.`member_id` AS `member_id`,`milsim_website_development`.`members_statuses`.`start_date` AS `start_date`,max(`milsim_website_development`.`members_statuses`.`id`) AS `max_id` from `milsim_website_development`.`members_statuses` group by `milsim_website_development`.`members_statuses`.`member_id`,`milsim_website_development`.`members_statuses`.`start_date`) `ls2` on(`ls2`.`member_id` = `ms`.`member_id` and `ls2`.`start_date` = `ms`.`start_date` and `ls2`.`max_id` = `ms`.`id`))) `ms` on(`ms`.`member_id` = `m`.`id`)) left join `milsim_website_development`.`statuses` `s` on(`s`.`id` = `ms`.`status_id`))
;

-- Removing temporary table and create final VIEW structure
DROP TABLE IF EXISTS `view_member_rank_status_all`;
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `view_member_rank_status_all` AS select `milsim_website_development`.`members`.`id` AS `member_id`,`milsim_website_development`.`members`.`name` AS `member_name`,`milsim_website_development`.`ranks`.`name` AS `rank`,`members_ranks`.`start_date` AS `rank_date`,`milsim_website_development`.`statuses`.`name` AS `status`,`members_statuses`.`start_date` AS `status_date` from ((((`milsim_website_development`.`members` left join (select `mr`.`id` AS `id`,`mr`.`member_id` AS `member_id`,`mr`.`rank_id` AS `rank_id`,`mr`.`start_date` AS `start_date`,`mr`.`created_at` AS `created_at`,`mr`.`updated_at` AS `updated_at` from (`milsim_website_development`.`members_ranks` `mr` join (select `milsim_website_development`.`members_ranks`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_ranks`.`start_date`) AS `max_rank_date` from `milsim_website_development`.`members_ranks` group by `milsim_website_development`.`members_ranks`.`member_id`) `latest_ranks` on(`mr`.`member_id` = `latest_ranks`.`member_id` and `mr`.`start_date` = `latest_ranks`.`max_rank_date`))) `members_ranks` on(`milsim_website_development`.`members`.`id` = `members_ranks`.`member_id`)) left join (select `ms`.`id` AS `id`,`ms`.`member_id` AS `member_id`,`ms`.`status_id` AS `status_id`,`ms`.`start_date` AS `start_date`,`ms`.`created_at` AS `created_at`,`ms`.`updated_at` AS `updated_at` from (`milsim_website_development`.`members_statuses` `ms` join (select `milsim_website_development`.`members_statuses`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_statuses`.`start_date`) AS `max_status_date` from `milsim_website_development`.`members_statuses` group by `milsim_website_development`.`members_statuses`.`member_id`) `latest_statuses` on(`ms`.`member_id` = `latest_statuses`.`member_id` and `ms`.`start_date` = `latest_statuses`.`max_status_date`))) `members_statuses` on(`milsim_website_development`.`members`.`id` = `members_statuses`.`member_id`)) left join `milsim_website_development`.`ranks` on(`members_ranks`.`rank_id` = `milsim_website_development`.`ranks`.`id`)) left join `milsim_website_development`.`statuses` on(`members_statuses`.`status_id` = `milsim_website_development`.`statuses`.`id`))
;

-- Removing temporary table and create final VIEW structure
DROP TABLE IF EXISTS `view_member_rank_unit_status_latest`;
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `view_member_rank_unit_status_latest` AS select `m`.`id` AS `member_id`,`m`.`name` AS `member_name`,`m`.`displayName` AS `displayName`,`r`.`name` AS `rank`,`mr`.`start_date` AS `rank_date`,`s`.`name` AS `status`,`ms`.`start_date` AS `status_date`,`u`.`name` AS `unit`,`mu`.`start_date` AS `unit_date`,`loa`.`loa_until` AS `loa_until` from (((((((`milsim_website_development`.`members` `m` left join (select `mr`.`member_id` AS `member_id`,`mr`.`rank_id` AS `rank_id`,`mr`.`start_date` AS `start_date` from (`milsim_website_development`.`members_ranks` `mr` join (select `milsim_website_development`.`members_ranks`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_ranks`.`start_date`) AS `max_start_date` from `milsim_website_development`.`members_ranks` group by `milsim_website_development`.`members_ranks`.`member_id`) `latest` on(`mr`.`member_id` = `latest`.`member_id` and `mr`.`start_date` = `latest`.`max_start_date`))) `mr` on(`m`.`id` = `mr`.`member_id`)) left join `milsim_website_development`.`ranks` `r` on(`mr`.`rank_id` = `r`.`id`)) left join (select `ms`.`member_id` AS `member_id`,`ms`.`status_id` AS `status_id`,`ms`.`start_date` AS `start_date` from (`milsim_website_development`.`members_statuses` `ms` join (select `milsim_website_development`.`members_statuses`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_statuses`.`start_date`) AS `max_start_date` from `milsim_website_development`.`members_statuses` group by `milsim_website_development`.`members_statuses`.`member_id`) `latest` on(`ms`.`member_id` = `latest`.`member_id` and `ms`.`start_date` = `latest`.`max_start_date`))) `ms` on(`m`.`id` = `ms`.`member_id`)) left join `milsim_website_development`.`statuses` `s` on(`ms`.`status_id` = `s`.`id`)) left join (select `mu`.`member_id` AS `member_id`,`mu`.`unit_id` AS `unit_id`,`mu`.`start_date` AS `start_date` from (`milsim_website_development`.`members_unit` `mu` join (select `milsim_website_development`.`members_unit`.`member_id` AS `member_id`,max(`milsim_website_development`.`members_unit`.`start_date`) AS `max_start_date` from `milsim_website_development`.`members_unit` group by `milsim_website_development`.`members_unit`.`member_id`) `latest` on(`mu`.`member_id` = `latest`.`member_id` and `mu`.`start_date` = `latest`.`max_start_date`))) `mu` on(`m`.`id` = `mu`.`member_id`)) left join `milsim_website_development`.`units` `u` on(`mu`.`unit_id` = `u`.`id`)) left join (select `l`.`member_id` AS `member_id`,max(greatest(cast(`l`.`end_date` as date),cast(coalesce(`l`.`extended_till`,`l`.`end_date`) as date))) AS `loa_until` from `milsim_website_development`.`leave_of_absences` `l` where `l`.`deleted` = 0 and (`l`.`closed` is null or `l`.`closed` = 0) and (`l`.`expired` is null or `l`.`expired` = 0) and cast(`l`.`start_date` as date) <= curdate() and (cast(`l`.`end_date` as date) >= curdate() or cast(`l`.`extended_till` as date) >= curdate()) group by `l`.`member_id`) `loa` on(`loa`.`member_id` = `m`.`id`))
;

-- Removing temporary table and create final VIEW structure
DROP TABLE IF EXISTS `view_member_settings`;
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `view_member_settings` AS select `m`.`id` AS `id`,`m`.`displayName` AS `displayName` from `members` `m`
;

/*!40103 SET TIME_ZONE=IFNULL(@OLD_TIME_ZONE, 'system') */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;
