-- Eggtracker schema — multi-user edition
-- This file runs automatically on first container startup only.
-- To re-run it, remove the mysql_data volume: docker compose down -v

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

USE eggtracker;

-- ── Users ─────────────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS users (
    id              INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    username        VARCHAR(64)     NOT NULL,
    hashed_password VARCHAR(255)    NOT NULL,
    is_admin        TINYINT(1)      NOT NULL DEFAULT 0,
    is_disabled     TINYINT(1)      NOT NULL DEFAULT 0,
    created_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_username (username)
) ENGINE=InnoDB;

-- ── Egg collections ───────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS egg_collections (
    id          INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    user_id     INT UNSIGNED    NOT NULL,
    date        DATE            NOT NULL,
    eggs        INT UNSIGNED    NOT NULL,
    notes       TEXT,
    created_at  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uq_user_date (user_id, date),
    INDEX idx_user_id (user_id),
    INDEX idx_date (date),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- ── Flock history ─────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS flock_history (
    id             INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    user_id        INT UNSIGNED    NOT NULL,
    date           DATE            NOT NULL,
    chicken_count  INT UNSIGNED    NOT NULL,
    notes          TEXT,
    created_at     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_user_id (user_id),
    INDEX idx_date (date),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- ── Feed purchases ────────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS feed_purchases (
    id             INT UNSIGNED      NOT NULL AUTO_INCREMENT,
    user_id        INT UNSIGNED      NOT NULL,
    date           DATE              NOT NULL,
    bags           DECIMAL(5, 2)     NOT NULL,
    price_per_bag  DECIMAL(10, 2)    NOT NULL,
    notes          TEXT,
    created_at     DATETIME          NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_user_id (user_id),
    INDEX idx_date (date),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;

-- ── Other purchases ───────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS other_purchases (
    id         INT UNSIGNED   NOT NULL AUTO_INCREMENT,
    user_id    INT UNSIGNED   NOT NULL,
    date       DATE           NOT NULL,
    total      DECIMAL(10, 2) NOT NULL,
    notes      TEXT,
    created_at DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_user_id (user_id),
    INDEX idx_date (date),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;
