-- Database of Dustin's Faults
-- Because we need to track this shit properly

CREATE DATABASE IF NOT EXISTS dustins_faults;
USE dustins_faults;

-- Table for tracking individual fuckups
CREATE TABLE IF NOT EXISTS fuckups (
    id INT AUTO_INCREMENT PRIMARY KEY,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    description TEXT NOT NULL,
    severity ENUM('minor', 'major', 'catastrophic', 'apocalyptic') DEFAULT 'major',
    blame_percentage INT DEFAULT 100 CHECK (blame_percentage BETWEEN 0 AND 100),
    dustin_excuse TEXT,
    created_by VARCHAR(255) DEFAULT 'Anonymous Blamer'
);

-- Table for Dustin's apologies (mostly empty)
CREATE TABLE IF NOT EXISTS apologies (
    id INT AUTO_INCREMENT PRIMARY KEY,
    fuckup_id INT,
    apology_text TEXT,
    sincerity_level ENUM('none', 'low', 'medium', 'high', 'bullshit') DEFAULT 'bullshit',
    apology_date DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (fuckup_id) REFERENCES fuckups(id) ON DELETE CASCADE
);

-- Table for blame statistics
CREATE TABLE IF NOT EXISTS blame_stats (
    id INT AUTO_INCREMENT PRIMARY KEY,
    date DATE UNIQUE,
    total_fuckups INT DEFAULT 0,
    avg_blame_percentage DECIMAL(5,2) DEFAULT 100.00,
    dustin_mood ENUM('defensive', 'apologetic', 'denial', 'blames_others') DEFAULT 'defensive'
);

-- Insert some initial data because Dustin has been busy
INSERT INTO fuckups (description, severity, dustin_excuse) VALUES
('Forgot to commit code before leaving for the day', 'catastrophic', 'My cat stepped on the keyboard'),
('Pushed to production on Friday at 4:55 PM', 'apocalyptic', 'I wanted to get a head start on Monday'),
('Deleted the production database instead of staging', 'apocalyptic', 'The buttons looked the same'),
('Spilled coffee on the server rack', 'major', 'The cup was slippery'),
('Set password to "password123" for "security"', 'major', 'I wanted to make it easy for everyone'),
('Scheduled maintenance during peak hours', 'catastrophic', 'I thought nobody would notice'),
('Used Comic Sans in the company presentation', 'minor', 'It looked friendly'),
('Accidentally replied all to company-wide email with "this meeting could have been an email"', 'major', 'My finger slipped');

-- Insert corresponding apologies (all bullshit, obviously)
INSERT INTO apologies (fuckup_id, apology_text, sincerity_level) VALUES
(1, 'Sorry not sorry, the cat made me do it', 'bullshit'),
(2, 'My bad, but have you tried working on weekends?', 'none'),
(3, 'Oopsie daisy! At least we have backups... right?', 'low'),
(4, 'The coffee was really good though, worth it', 'none'),
(5, 'Security through obscurity is overrated', 'bullshit'),
(6, 'I was optimizing for my own schedule', 'none'),
(7, 'Comic Sans is underappreciated', 'medium'),
(8, 'We all were thinking it, I just said it', 'high');

-- Create a view for the worst offenses
CREATE VIEW worst_fuckups AS
SELECT 
    f.*,
    a.apology_text,
    a.sincerity_level
FROM fuckups f
LEFT JOIN apologies a ON f.id = a.fuckup_id
WHERE f.severity IN ('catastrophic', 'apocalyptic')
ORDER BY f.timestamp DESC;

-- Create a stored procedure to add new fuckups
DELIMITER //
CREATE PROCEDURE add_fuckup(
    IN p_description TEXT,
    IN p_severity VARCHAR(20),
    IN p_excuse TEXT,
    IN p_blamer VARCHAR(255)
)
BEGIN
    INSERT INTO fuckups (description, severity, dustin_excuse, created_by)
    VALUES (p_description, p_severity, p_excuse, p_blamer);
    
    -- Auto-add a bullshit apology
    INSERT INTO apologies (fuckup_id, apology_text, sincerity_level)
    VALUES (LAST_INSERT_ID(), 
            CONCAT('I''m sorry you feel that way about ', p_description),
            'bullshit');
END //
DELIMITER ;

-- Create an event to auto-add daily fuckups (because Dustin is consistent)
DELIMITER //
CREATE EVENT daily_dustin_fuckup
ON SCHEDULE EVERY 1 DAY
STARTS CURRENT_TIMESTAMP
DO
BEGIN
    CALL add_fuckup(
        CONCAT('Daily fuckup #', FLOOR(RAND() * 1000)),
        ELT(FLOOR(RAND() * 4) + 1, 'minor', 'major', 'catastrophic', 'apocalyptic'),
        ELT(FLOOR(RAND() * 5) + 1, 'It was Tuesday', 'The stars were misaligned', 'I was hungry', 'My horoscope said so', 'Aliens'),
        'Auto-Blame-Bot 3000'
    );
END //
DELIMITER ;