CREATE TYPE fruit_type AS ENUM (
  'Apfelsorten',
  'Birnensorten',
  'Quittensorten',
  'Aprikosen',
  'Pfirsiche',
  'Mirabellen',
  'Renekloden',
  'Pflaumen',
  'Zwetschen',
  'Sauerkirschen',
  'Süßkirschen',
  'Brombeeren',
  'Erdbeeren',
  'Himbeeren',
  'Johannisbeeren',
  'Stachelbeeren',
  'Wein'
);

CREATE TABLE fruits (
  id          SERIAL       PRIMARY KEY,
  name        VARCHAR(255) NOT NULL,
  osdb_number VARCHAR(50)  NOT NULL UNIQUE,
  comment     TEXT,
  fruit_type  fruit_type   NOT NULL,
  created_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
  updated_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE TABLE fruit_synonyms (
  id       SERIAL       PRIMARY KEY,
  fruit_id INTEGER      NOT NULL REFERENCES fruits(id) ON DELETE CASCADE,
  synonym  VARCHAR(255) NOT NULL
);

CREATE TABLE fruit_images (
  id         SERIAL       PRIMARY KEY,
  fruit_id   INTEGER      NOT NULL REFERENCES fruits(id) ON DELETE CASCADE,
  filename   VARCHAR(255),
  data       BYTEA        NOT NULL,
  image_type VARCHAR(20)  NOT NULL CHECK (image_type IN ('fruit', 'flower', 'tree')),
  title      VARCHAR(255),
  created_at TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);
