-- Sproutly Database Schema

CREATE TABLE IF NOT EXISTS varieties (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    variety_name VARCHAR(100),
    category ENUM('vegetable', 'herb', 'flower', 'fruit') DEFAULT 'vegetable',
    weeks_to_start INT COMMENT 'Weeks before last frost to start seeds indoors',
    weeks_to_greenhouse INT COMMENT 'Weeks before last frost to pot up or move to greenhouse',
    weeks_to_garden INT COMMENT 'Weeks after last frost to transplant outdoors (negative = before frost)',
    days_to_germinate INT DEFAULT 7,
    direct_sow_ok BOOLEAN DEFAULT FALSE,
    frost_tolerant BOOLEAN DEFAULT FALSE,
    sun_requirement ENUM('full_sun', 'part_shade', 'full_shade') DEFAULT 'full_sun',
    water_needs ENUM('low', 'medium', 'high') DEFAULT 'medium',
    color VARCHAR(7) DEFAULT '#52b788',
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS batches (
    id INT AUTO_INCREMENT PRIMARY KEY,
    variety_id INT NOT NULL,
    label VARCHAR(100),
    quantity INT DEFAULT 1,
    sow_date DATE,
    germination_date DATE,
    greenhouse_date DATE,
    garden_date DATE,
    status ENUM('planned','germinating','seedling','potted_up','hardening','garden','harvested','failed') DEFAULT 'planned',
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (variety_id) REFERENCES varieties(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS settings (
    id INT PRIMARY KEY DEFAULT 1,
    last_frost_date DATE,
    first_frost_fall_date DATE,
    ntfy_topic VARCHAR(200),
    ntfy_server VARCHAR(200) DEFAULT 'https://ntfy.sh',
    notification_time VARCHAR(5) DEFAULT '07:00',
    timezone VARCHAR(50) DEFAULT 'UTC',
    location_name VARCHAR(100)
);

CREATE TABLE IF NOT EXISTS notification_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    message TEXT,
    status VARCHAR(20),
    error TEXT
);

-- Insert default settings row
INSERT INTO settings (id) VALUES (1);

-- Sample plant varieties
INSERT INTO varieties (name, variety_name, category, weeks_to_start, weeks_to_greenhouse, weeks_to_garden, days_to_germinate, frost_tolerant, sun_requirement, water_needs, color, notes) VALUES
('Tomato', 'Roma', 'vegetable', 8, 2, 2, 7, FALSE, 'full_sun', 'medium', '#e76f51', 'Start indoors 6-8 weeks before last frost. Needs warm soil to transplant.'),
('Tomato', 'Cherry', 'vegetable', 8, 2, 2, 7, FALSE, 'full_sun', 'medium', '#f4a261', 'Great in containers. Very prolific producer.'),
('Pepper', 'Bell', 'vegetable', 10, 2, 2, 10, FALSE, 'full_sun', 'medium', '#e9c46a', 'Slow to germinate, start early. Needs heat.'),
('Pepper', 'Hot Banana', 'vegetable', 10, 2, 2, 12, FALSE, 'full_sun', 'low', '#f4a261', 'Very slow to germinate. Keep soil warm (80F+).'),
('Broccoli', 'Calabrese', 'vegetable', 6, 2, -2, 5, TRUE, 'full_sun', 'medium', '#2d6a4f', 'Can tolerate light frost. Start indoors for spring or direct sow in summer for fall crop.'),
('Lettuce', 'Butterhead', 'vegetable', 4, 1, -4, 3, TRUE, 'part_shade', 'medium', '#74c69d', 'Cold tolerant. Can direct sow early in spring. Bolts in heat.'),
('Cucumber', 'Straight Eight', 'vegetable', 3, 0, 2, 5, FALSE, 'full_sun', 'high', '#52b788', 'Direct sow after last frost or start indoors 2-3 weeks before. Hates root disturbance.'),
('Basil', 'Sweet', 'herb', 6, 1, 2, 7, FALSE, 'full_sun', 'medium', '#40916c', 'Very frost sensitive. Start indoors, transplant after all danger of frost.'),
('Marigold', 'French', 'flower', 6, 1, 0, 5, FALSE, 'full_sun', 'low', '#f4a261', 'Great companion plant for tomatoes. Deters pests.'),
('Zinnia', 'Cut & Come Again', 'flower', 4, 0, 1, 5, FALSE, 'full_sun', 'low', '#e76f51', 'Can direct sow after last frost. Easy and prolific.'),
('Kale', 'Lacinato', 'vegetable', 6, 2, -4, 5, TRUE, 'full_sun', 'medium', '#1b4332', 'Very cold hardy. Start early for spring or late summer for fall/winter harvest.'),
('Squash', 'Zucchini', 'vegetable', 3, 0, 2, 5, FALSE, 'full_sun', 'high', '#95d5b2', 'Direct sow or start indoors 2-3 weeks before last frost. Fast growing.');
