-- ============================================
-- PlusFit24 Datenbank Schema
-- Auf dem MariaDB Server ausführen
-- ============================================

CREATE DATABASE IF NOT EXISTS plusfit24 
  CHARACTER SET utf8mb4 
  COLLATE utf8mb4_unicode_ci;

USE plusfit24;

-- Kategorien Tabelle
CREATE TABLE IF NOT EXISTS categories (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO categories (name) VALUES
('Single'),
('Schüler / Studenten / Vereine'),
('Family'),
('Sonstige');

-- Tarife Tabelle
CREATE TABLE IF NOT EXISTS tariffs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  category_id INT,
  duration_months INT NOT NULL,
  price_monthly DECIMAL(10,2) NOT NULL,
  start_package_price DECIMAL(10,2) DEFAULT 35.00,
  description TEXT,
  active TINYINT(1) DEFAULT 1,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL
);

INSERT INTO tariffs (name, category_id, duration_months, price_monthly, start_package_price, description) VALUES
('Single 12 Monate', 1, 12, 29.95, 35.00, 'Einzelmitgliedschaft für 12 Monate'),
('Single 24 Monate', 1, 24, 19.95, 35.00, 'Einzelmitgliedschaft für 24 Monate'),
('Schüler - Studenten - Vereine 12 Monate', 2, 12, 28.95, 35.00, 'Ermäßigter Tarif'),
('Family 24 Monate', 3, 24, 49.95, 35.00, 'Familientarif für 24 Monate');

-- Mitgliedschaften Tabelle
CREATE TABLE IF NOT EXISTS memberships (
  id INT AUTO_INCREMENT PRIMARY KEY,
  tariff_id INT NOT NULL,
  salutation VARCHAR(20) NOT NULL,
  title VARCHAR(50),
  first_name VARCHAR(100) NOT NULL,
  last_name VARCHAR(100) NOT NULL,
  birth_date DATE NOT NULL,
  email VARCHAR(255) NOT NULL,
  phone VARCHAR(50),
  street VARCHAR(200) NOT NULL,
  address_addition VARCHAR(200),
  zip VARCHAR(10) NOT NULL,
  city VARCHAR(100) NOT NULL,
  bank_name VARCHAR(200),
  account_holder VARCHAR(200),
  iban VARCHAR(34),
  sepa_accepted TINYINT(1) DEFAULT 0,
  agb_accepted TINYINT(1) DEFAULT 0,
  datenschutz_accepted TINYINT(1) DEFAULT 0,
  data_correct TINYINT(1) DEFAULT 0,
  guardian_consent TINYINT(1) DEFAULT 0,
  is_minor TINYINT(1) DEFAULT 0,
  status VARCHAR(20) DEFAULT 'active',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (tariff_id) REFERENCES tariffs(id)
);

-- Admin Tabelle
CREATE TABLE IF NOT EXISTS admins (
  id INT AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(100) NOT NULL UNIQUE,
  password_hash VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_memberships_email ON memberships(email);
CREATE INDEX idx_memberships_status ON memberships(status);
CREATE INDEX idx_tariffs_active ON tariffs(active);
