-- phpMyAdmin SQL Dump
-- version 5.2.3
-- https://www.phpmyadmin.net/

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_abteilungen`
--

CREATE TABLE `prefix-placeholder_abteilungen` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL DEFAULT 'KEIN NAME'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_audiofiles`
--

CREATE TABLE `prefix-placeholder_audiofiles` (
  `id` int(11) NOT NULL,
  `file_name` varchar(1024) NOT NULL,
  `file_path` varchar(1024) NOT NULL,
  `delete_key` binary(16) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_basket_items`
--

CREATE TABLE `prefix-placeholder_basket_items` (
  `id` int(11) NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `item_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_geraete`
--

CREATE TABLE `prefix-placeholder_geraete` (
  `id` int(11) NOT NULL,
  `name` varchar(1024) DEFAULT NULL,
  `start_index` int(11) DEFAULT NULL,
  `color_kampfrichter` char(7) NOT NULL DEFAULT '#424242'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_intern_users`
--

CREATE TABLE `prefix-placeholder_intern_users` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `username` varchar(191) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `password_cipher` text NOT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `edited_by` varchar(20) NOT NULL DEFAULT '0',
  `freigabe` varchar(500) NOT NULL DEFAULT 'keine'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_kampfricherinnen_protokoll`
--

CREATE TABLE `prefix-placeholder_kampfricherinnen_protokoll` (
  `id` int(11) NOT NULL,
  `abteilung` int(11) NOT NULL DEFAULT 0,
  `geraet` varchar(10) NOT NULL DEFAULT 'NaN',
  `name` varchar(255) NOT NULL DEFAULT 'NaN',
  `aufgabe` int(255) NOT NULL DEFAULT 3
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_noten`
--

CREATE TABLE `prefix-placeholder_noten` (
  `person_id` int(11) NOT NULL,
  `note_bezeichnung_id` int(11) NOT NULL,
  `geraet_id` int(11) NOT NULL,
  `jahr` year(4) NOT NULL,
  `run_number` TINYINT(3) NOT NULL DEFAULT 1,
  `value` int(11) DEFAULT NULL,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_noten_bezeichnungen`
--

CREATE TABLE `prefix-placeholder_noten_bezeichnungen` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `default_value` float DEFAULT NULL,
  `type` enum('input','berechnung') NOT NULL DEFAULT 'input',
  `berechnung` varchar(255) DEFAULT NULL,
  `berechnung_json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`berechnung_json`)),
  `max_value` float DEFAULT NULL,
  `min_value` float DEFAULT NULL,
  `pro_geraet` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_onetimeloginlinks`
--

CREATE TABLE `prefix-placeholder_onetimeloginlinks` (
  `id` int(11) NOT NULL,
  `url` varchar(1000) DEFAULT NULL,
  `user_id` int(11) NOT NULL DEFAULT 0,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

--
-- Trigger `prefix-placeholder_onetimeloginlinks`
--
DELIMITER $$
CREATE TRIGGER `before_insert_login_tokens` BEFORE INSERT ON `prefix-placeholder_onetimeloginlinks` FOR EACH ROW BEGIN
    IF NEW.url IS NULL THEN
        SET NEW.url = HEX(RANDOM_BYTES(32));
    END IF;
END
$$
DELIMITER ;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_orders`
--

CREATE TABLE `prefix-placeholder_orders` (
  `order_id` int(11) NOT NULL,
  `order_type` varchar(100) DEFAULT NULL,
  `preis` float NOT NULL,
  `user_id` int(11) NOT NULL,
  `item_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`item_ids`)),
  `order_status` int(11) NOT NULL DEFAULT 0,
  `timestamp` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

--
-- Trigger `prefix-placeholder_orders`
--
DELIMITER $$
CREATE TRIGGER `tr_update_turnerinnen_bezahlt` AFTER UPDATE ON `prefix-placeholder_orders` FOR EACH ROW BEGIN
    -- Only when status changes
    IF NEW.order_status <> OLD.order_status THEN
        
        UPDATE prefix-placeholder_turnerinnen AS t
        SET bezahlt = NEW.order_status
        WHERE JSON_CONTAINS(NEW.item_ids, JSON_QUOTE(CAST(t.id AS CHAR)));
    
    END IF;
END
$$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER `trigger02` BEFORE DELETE ON `prefix-placeholder_orders` FOR EACH ROW BEGIN
        UPDATE prefix-placeholder_turnerinnen AS t
        SET bezahlt = 0
        WHERE JSON_CONTAINS(OLD.item_ids, JSON_QUOTE(CAST(t.id AS CHAR)));
END
$$
DELIMITER ;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_programme`
--

CREATE TABLE `prefix-placeholder_programme` (
  `id` int(11) NOT NULL,
  `programm` text NOT NULL,
  `preis` decimal(10,2) DEFAULT 0.00,
  `aktiv` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_turnerinnen`
--

CREATE TABLE `prefix-placeholder_turnerinnen` (
  `id` int(11) NOT NULL,
  `name` text NOT NULL,
  `vorname` text NOT NULL,
  `geburtsdatum` date NOT NULL,
  `programm` text NOT NULL,
  `verein` text NOT NULL,
  `gesamtpunktzahl` float NOT NULL DEFAULT 40,
  `rang` int(11) NOT NULL DEFAULT 0,
  `bezahlt` int(11) NOT NULL DEFAULT 0,
  `bezahltoverride` int(11) NOT NULL DEFAULT 0,
  `bodenmusik` int(11) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_turnerinnen_abteilungen`
--

CREATE TABLE `prefix-placeholder_turnerinnen_abteilungen` (
  `id` int(11) NOT NULL,
  `turnerin_id` int(11) NOT NULL,
  `abteilung_id` int(11) NOT NULL,
  `geraet_id` int(11) NOT NULL,
  `turnerin_index` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_variables`
--

CREATE TABLE `prefix-placeholder_variables` (
  `name` varchar(255) NOT NULL DEFAULT 'NaN',
  `value` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_vereine`
--

CREATE TABLE `prefix-placeholder_vereine` (
  `id` int(11) NOT NULL,
  `verein` text NOT NULL,
  `email` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_zeitplan`
--

CREATE TABLE `prefix-placeholder_zeitplan` (
  `id` int(11) NOT NULL,
  `typ_id` int(11) NOT NULL,
  `start_time_date` datetime NOT NULL,
  `end_time_date` datetime NOT NULL,
  `abt_programm` int(11) DEFAULT NULL,
  `name` varchar(1024) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

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

--
-- Tabellenstruktur für Tabelle `prefix-placeholder_zeitplan_types`
--

CREATE TABLE `prefix-placeholder_zeitplan_types` (
  `id` int(11) NOT NULL,
  `name` varchar(2048) NOT NULL,
  `duration_fixed` time DEFAULT NULL,
  `duration_per_person` time DEFAULT NULL,
  `duration_min` time DEFAULT NULL,
  `duration_max` time DEFAULT NULL,
  `ort` varchar(1024) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `prefix-placeholder_abteilungen`
--
ALTER TABLE `prefix-placeholder_abteilungen`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_audiofiles`
--
ALTER TABLE `prefix-placeholder_audiofiles`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_basket_items`
--
ALTER TABLE `prefix-placeholder_basket_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `prefix-placeholder_basket_items_ibfk_1` (`user_id`),
  ADD KEY `prefix-placeholder_basket_items_ibfk_2` (`item_id`);

--
-- Indizes für die Tabelle `prefix-placeholder_geraete`
--
ALTER TABLE `prefix-placeholder_geraete`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_intern_users`
--
ALTER TABLE `prefix-placeholder_intern_users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `username_page_unique` (`username`,`password_hash`) USING BTREE,
  ADD KEY `indexFreigabe` (`freigabe`);

--
-- Indizes für die Tabelle `prefix-placeholder_kampfricherinnen_protokoll`
--
ALTER TABLE `prefix-placeholder_kampfricherinnen_protokoll`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_noten`
--
ALTER TABLE `prefix-placeholder_noten`
  ADD UNIQUE KEY `uniquePersonNoteJahrGereat` (`person_id`,`note_bezeichnung_id`,`jahr`,`geraet_id`,`run_number`) USING BTREE;

--
-- Indizes für die Tabelle `prefix-placeholder_noten_bezeichnungen`
--
ALTER TABLE `prefix-placeholder_noten_bezeichnungen`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uniqueName` (`name`) USING BTREE,
  ADD KEY `indexMaxValue` (`max_value`),
  ADD KEY `indexMinValue` (`min_value`),
  ADD KEY `indexBerechnungJson` (`berechnung_json`(768));

--
-- Indizes für die Tabelle `prefix-placeholder_onetimeloginlinks`
--
ALTER TABLE `prefix-placeholder_onetimeloginlinks`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `url` (`url`);

--
-- Indizes für die Tabelle `prefix-placeholder_orders`
--
ALTER TABLE `prefix-placeholder_orders`
  ADD PRIMARY KEY (`order_id`);

--
-- Indizes für die Tabelle `prefix-placeholder_programme`
--
ALTER TABLE `prefix-placeholder_programme`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_turnerinnen`
--
ALTER TABLE `prefix-placeholder_turnerinnen`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_turnerinnen_abteilungen`
--
ALTER TABLE `prefix-placeholder_turnerinnen_abteilungen`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_turnerin` (`turnerin_id`);

--
-- Indizes für die Tabelle `prefix-placeholder_variables`
--
ALTER TABLE `prefix-placeholder_variables`
  ADD UNIQUE KEY `uniqueIndex` (`name`);

--
-- Indizes für die Tabelle `prefix-placeholder_vereine`
--
ALTER TABLE `prefix-placeholder_vereine`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `indexturnerinnen` (`id`,`verein`(31));

--
-- Indizes für die Tabelle `prefix-placeholder_zeitplan`
--
ALTER TABLE `prefix-placeholder_zeitplan`
  ADD PRIMARY KEY (`id`);

--
-- Indizes für die Tabelle `prefix-placeholder_zeitplan_types`
--
ALTER TABLE `prefix-placeholder_zeitplan_types`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT für exportierte Tabellen
--

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_abteilungen`
--
ALTER TABLE `prefix-placeholder_abteilungen`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_audiofiles`
--
ALTER TABLE `prefix-placeholder_audiofiles`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_basket_items`
--
ALTER TABLE `prefix-placeholder_basket_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_geraete`
--
ALTER TABLE `prefix-placeholder_geraete`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_intern_users`
--
ALTER TABLE `prefix-placeholder_intern_users`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_kampfricherinnen_protokoll`
--
ALTER TABLE `prefix-placeholder_kampfricherinnen_protokoll`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_noten_bezeichnungen`
--
ALTER TABLE `prefix-placeholder_noten_bezeichnungen`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_onetimeloginlinks`
--
ALTER TABLE `prefix-placeholder_onetimeloginlinks`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_programme`
--
ALTER TABLE `prefix-placeholder_programme`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_turnerinnen`
--
ALTER TABLE `prefix-placeholder_turnerinnen`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_turnerinnen_abteilungen`
--
ALTER TABLE `prefix-placeholder_turnerinnen_abteilungen`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_vereine`
--
ALTER TABLE `prefix-placeholder_vereine`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_zeitplan`
--
ALTER TABLE `prefix-placeholder_zeitplan`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT für Tabelle `prefix-placeholder_zeitplan_types`
--
ALTER TABLE `prefix-placeholder_zeitplan_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;


--
-- Erstellt einen ersten Benutzer
--
INSERT INTO `prefix-placeholder_intern_users` 
  (`usename`, `pw_hash`, `freigaben`) 
  VALUES ('admin', 'PW_HASH', '{"types":["wk_leitung"]}');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
