-- Enhanced Joke Bot Database Schema with User Identification
-- Version 4.0 - User Analytics Edition

-- Drop existing tables if they exist (clean slate)
DROP TABLE IF EXISTS user_sentiments;
DROP TABLE IF EXISTS jokes;

-- Create jokes table
CREATE TABLE jokes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    joke TEXT NOT NULL,
    contributor TEXT NOT NULL,
    created_date TEXT NOT NULL,
    approved BOOLEAN DEFAULT 0,
    sentiment_score REAL DEFAULT 0.0,
    sentiment_label TEXT DEFAULT '😐 Neutral'       
);

-- Create user_sentiments table with user identification
CREATE TABLE user_sentiments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    joke_id INTEGER NOT NULL,
    user_sentiment TEXT CHECK(user_sentiment IN ('up', 'down', 'neutral')) DEFAULT 'neutral',
    user_identifier TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (joke_id) REFERENCES jokes(id) ON DELETE CASCADE
);

-- Insert sample jokes with approval status
INSERT INTO jokes (joke, contributor, created_date, approved) VALUES
('Why don''t scientists trust atoms? Because they make up everything!', 'ScienceFan', '2024-01-15 10:30:00', 1),
('I told my wife she was drawing her eyebrows too high. She looked surprised.', 'Joker123', '2024-01-16 14:20:00', 1),
('Why did the scarecrow win an award? He was outstanding in his field!', 'FarmLife', '2024-01-17 09:15:00', 1),
('What do you call a fish with no eyes? Fsh!', 'MarineBio', '2024-01-18 16:45:00', 1),
('I''m reading a book on anti-gravity. It''s impossible to put down!', 'PhysicsNerd', '2024-01-19 11:30:00', 1),
('Why did the computer go to the doctor? Because it had a virus.', 'TechSupport', '2024-01-20 13:10:00', 1),
('What do you call a bear with no teeth? A gummy bear.', 'WildlifeFan', '2024-01-21 15:25:00', 1),
('Why did the bicycle fall over? Because it was two-tired.', 'Cyclist', '2024-01-22 10:00:00', 1),
('What do you call a sleeping bull? A bulldozer.', 'Cowboy', '2024-01-23 14:35:00', 1),
('Why did the math book look so sad? Because it had too many problems.', 'Student', '2024-01-24 09:50:00', 1);

-- Insert sample user sentiments with user identifiers
INSERT INTO user_sentiments (joke_id, user_sentiment, user_identifier) VALUES
(1, 'up', 'ComedyFan'),
(1, 'up', 'JokeLover'), 
(1, 'neutral', 'CritiqueMaster'),
(1, 'up', 'FunnyPerson'),
(2, 'down', 'SeriousReader'),
(2, 'up', 'HappyViewer'),
(2, 'neutral', 'NeutralObserver'),
(3, 'up', 'ComedyFan'),
(3, 'up', 'JokeLover'),
(3, 'up', 'FunnyPerson'),
(3, 'down', 'CritiqueMaster'),
(4, 'neutral', 'FishExpert'),
(4, 'down', 'GrammarNazi'),
(4, 'up', 'PunLover'),
(5, 'up', 'ScienceGeek'),
(5, 'up', 'BookWorm'),
(5, 'neutral', 'Skeptic'),
(6, 'up', 'TechEnthusiast'),
(6, 'down', 'ComputerHater'),
(7, 'up', 'AnimalLover'),
(7, 'up', 'WordPlayFan'),
(8, 'up', 'CyclingFan'),
(8, 'neutral', 'BikeNovice'),
(9, 'up', 'FarmKid'),
(9, 'down', 'CitySlicker'),
(10, 'neutral', 'MathStudent'),
(10, 'up', 'ProblemSolver');

-- Verification queries
SELECT '✅ Database setup complete!' as status;

-- Show joke counts
SELECT 'Total jokes in database:' as info, COUNT(*) as count FROM jokes;

-- Show sentiment distribution
SELECT 
    'Sentiment Distribution:' as info,
    user_sentiment,
    COUNT(*) as count
FROM user_sentiments
GROUP BY user_sentiment
ORDER BY count DESC;

-- Show most active users
SELECT 
    'Most Active Users:' as info,
    user_identifier,
    COUNT(*) as ratings_given
FROM user_sentiments
GROUP BY user_identifier
ORDER BY ratings_given DESC
LIMIT 5;

-- Show community positivity by joke
SELECT 
    'Community Positivity by Joke:' as info,
    j.joke,
    ROUND(AVG(CASE WHEN us.user_sentiment = 'up' THEN 1.0 
                   WHEN us.user_sentiment = 'down' THEN 0.0 
                   ELSE 0.5 END) * 100, 1) as positivity_percent,
    COUNT(*) as total_ratings
FROM jokes j
JOIN user_sentiments us ON j.id = us.joke_id
GROUP BY j.id, j.joke
ORDER BY positivity_percent DESC;