-- ABOUTME: Migration 0001 - reference-tables
-- ABOUTME: Creates the five reference tables: species, locations, products, feed_types, users.

-- Species: lookup table for animal species (duck, goose, sheep)
CREATE TABLE species (
    code TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
    created_at_utc INTEGER NOT NULL,
    updated_at_utc INTEGER NOT NULL
);

-- Locations: physical locations (Strip 1-4, Nursery 1-4)
CREATE TABLE locations (
    id TEXT PRIMARY KEY CHECK(length(id) = 26),
    name TEXT NOT NULL UNIQUE,
    active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
    created_at_utc INTEGER NOT NULL,
    updated_at_utc INTEGER NOT NULL
);

-- Products: things that can be collected/sold (eggs, meat, etc.)
CREATE TABLE products (
    code TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    unit TEXT NOT NULL CHECK(unit IN ('piece', 'kg')),
    collectable INTEGER NOT NULL CHECK(collectable IN (0, 1)),
    sellable INTEGER NOT NULL CHECK(sellable IN (0, 1)),
    active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
    created_at_utc INTEGER NOT NULL,
    updated_at_utc INTEGER NOT NULL
);

-- Feed types: types of feed (starter, grower, layer)
CREATE TABLE feed_types (
    code TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    default_bag_size_kg INTEGER NOT NULL CHECK(default_bag_size_kg >= 1),
    protein_pct REAL NULL,
    active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
    created_at_utc INTEGER NOT NULL,
    updated_at_utc INTEGER NOT NULL
);

-- Users: system users with roles
CREATE TABLE users (
    username TEXT PRIMARY KEY,
    role TEXT NOT NULL CHECK(role IN ('admin', 'recorder')),
    active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
    created_at_utc INTEGER NOT NULL,
    updated_at_utc INTEGER NOT NULL
);
