BEGIN;

CREATE SCHEMA IF NOT EXISTS training;

CREATE TABLE IF NOT EXISTS training.trainings (
  id          UUID PRIMARY KEY,
  name        varchar(255) NOT NULL,
  description text NOT NULL,
  days        smallint NOT NULL
);

CREATE TABLE IF NOT EXISTS training.prices (
  training_id UUID REFERENCES training.trainings(id),
  amount NUMERIC(10,4) NOT NULL,
  currency VARCHAR(3) NOT NULL,
  CONSTRAINT positive_amount CHECK (amount >= 0),
  CONSTRAINT allowed_currencies CHECK (currency IN ('USD', 'EUR', 'CZK')),
  type        VARCHAR(10) NOT NULL,
  CHECK       (type IN ('OPEN', 'CORPORATE')),
  PRIMARY KEY (training_id, currency, type) -- composite primary key
);

CREATE TABLE IF NOT EXISTS training.dates (
  ID UUID PRIMARY KEY,
  training_id UUID REFERENCES training.trainings(id),
  date DATE NOT NULL,
  start_time TIME NOT NULL,
  days SMALLINT NOT NULL,
  is_online BOOLEAN NOT NULL,
  location VARCHAR(255) NOT NULL,
  address VARCHAR(500) NOT NULL,
  capacity SMALLINT NOT NULL,
  price_amount NUMERIC(10,4) NOT NULL,
  price_currency VARCHAR(3) NOT NULL,
  CONSTRAINT positive_amount CHECK (price_amount >= 0),
  CONSTRAINT allowed_currencies CHECK (price_currency IN ('USD', 'EUR', 'CZK'))
);

CREATE TABLE IF NOT EXISTS training.date_attendees (
  id UUID PRIMARY KEY,
  date_id UUID REFERENCES training.dates(id),
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  phone VARCHAR(20) NOT NULL,
  company VARCHAR(255) NOT NULL,
  position VARCHAR(255) NOT NULL,
  is_student BOOLEAN NOT NULL,
  has_paid BOOLEAN NOT NULL,
  has_attended BOOLEAN NOT NULL,
  bill_amount NUMERIC(10,4) NOT NULL,
  bill_currency VARCHAR(3) NOT NULL,
  CONSTRAINT positive_amount CHECK (bill_amount >= 0),
  CONSTRAINT allowed_currencies CHECK (bill_currency IN ('USD', 'EUR', 'CZK'))
);

COMMIT;
