CREATE TYPE frost_tolerance AS ENUM ('none', 'light_frost', 'moderate_frost', 'hardy');

CREATE TABLE cultivars (
    id                  UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    slug                TEXT NOT NULL UNIQUE,
    species_id          UUID NOT NULL REFERENCES species(id) ON DELETE RESTRICT,
    name                TEXT NOT NULL,
    name_en             TEXT,
    name_de             TEXT,
    name_scientific     TEXT,
    description         TEXT,
    is_organic          BOOLEAN NOT NULL DEFAULT FALSE,
    perennial           BOOLEAN NOT NULL DEFAULT FALSE,
    growing_time_days   INTEGER,
    planting_depth_cm   DOUBLE PRECISION,
    row_spacing_cm      DOUBLE PRECISION,
    plant_spacing_cm    DOUBLE PRECISION,
    days_to_germination INTEGER,
    germination_temp_c  DOUBLE PRECISION,
    light_requirement   TEXT,
    stratification_required BOOLEAN,
    stratification_days INTEGER,
    scarification_required  BOOLEAN,
    seed_viability_years INTEGER,
    storage_temp_c       DOUBLE PRECISION,
    storage_humidity     TEXT,
    storage_notes        TEXT,
    min_temp            DOUBLE PRECISION,
    max_temp            DOUBLE PRECISION,
    humidity            TEXT,
    light               TEXT,
    frost_tolerance     frost_tolerance,
    min_light_hours_day      DOUBLE PRECISION,
    optimal_light_hours_day  DOUBLE PRECISION,
    greenhouse_min_temp_c    DOUBLE PRECISION,
    indoor_season_extension_weeks INTEGER,
    ventilation_requirement  TEXT,
    heating_required         BOOLEAN,
    indoor_sowing_months    INTEGER[],
    direct_sowing_months    INTEGER[],
    transplanting_months    INTEGER[],
    glasshouse_months       INTEGER[],
    harvesting_months       INTEGER[],
    succession_planting_days INTEGER,
    planting_notes          TEXT,
    propagation_methods     TEXT[],
    cutting_season          TEXT,
    rootstock_species_id    UUID REFERENCES species(id),
    years_to_first_harvest  INTEGER,
    productive_lifespan_years INTEGER,
    expected_yield_kg_per_m2 DOUBLE PRECISION,
    yield_unit              TEXT,
    expected_yield_value    DOUBLE PRECISION,
    harvest_window_days     INTEGER,
    storage_method          TEXT[],
    shelf_life_days         INTEGER,
    cold_storage_days       INTEGER,
    pollination_group   TEXT,
    self_fertile        BOOLEAN,
    rootstock_compatibility TEXT,
    wikidata_qid        TEXT,
    gbif_id             TEXT,
    pfaf_url            TEXT,
    primary_image_key   TEXT,
    source_urls         TEXT[],
    created_at          TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_cultivars_species ON cultivars(species_id);
CREATE INDEX idx_cultivars_rootstock ON cultivars(rootstock_species_id) WHERE rootstock_species_id IS NOT NULL;
CREATE INDEX idx_cultivars_search ON cultivars
    USING GIN (to_tsvector('english',
        coalesce(name,'') || ' ' ||
        coalesce(name_en,'') || ' ' ||
        coalesce(name_de,'') || ' ' ||
        coalesce(name_scientific,'') || ' ' ||
        coalesce(description,'')));
