-- Create categories table
CREATE TABLE IF NOT EXISTS `categories` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(255) NOT NULL UNIQUE
);

-- Create questions table
CREATE TABLE IF NOT EXISTS `questions` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `category_id` INT NOT NULL,
  `question_text` TEXT NOT NULL,
  FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE CASCADE
);

-- Create answers table
CREATE TABLE IF NOT EXISTS `answers` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `question_id` INT NOT NULL,
  `answer_text` VARCHAR(255) NOT NULL,
  `is_correct` BOOLEAN NOT NULL DEFAULT 0,
  FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON DELETE CASCADE
);

-- Insert categories
INSERT INTO `categories` (`name`) VALUES ('Cultura'), ('Tradizione')
ON DUPLICATE KEY UPDATE `name` = `name`;

-- Insert questions and answers for "Cultura"
-- Question 1
INSERT INTO `questions` (`category_id`, `question_text`) SELECT id, 'Quale di questi monumenti si trova a Roma?' FROM `categories` WHERE `name` = 'Cultura';
SET @last_question_id = LAST_INSERT_ID();
INSERT INTO `answers` (`question_id`, `answer_text`, `is_correct`) VALUES
(@last_question_id, 'La Torre di Pisa', 0),
(@last_question_id, 'Il Colosseo', 1),
(@last_question_id, 'Il Duomo di Milano', 0),
(@last_question_id, 'Il Ponte di Rialto', 0);

-- Question 2
INSERT INTO `questions` (`category_id`, `question_text`) SELECT id, 'Chi ha scritto "La Divina Commedia"?' FROM `categories` WHERE `name` = 'Cultura';
SET @last_question_id = LAST_INSERT_ID();
INSERT INTO `answers` (`question_id`, `answer_text`, `is_correct`) VALUES
(@last_question_id, 'Alessandro Manzoni', 0),
(@last_question_id, 'Giacomo Leopardi', 0),
(@last_question_id, 'Dante Alighieri', 1),
(@last_question_id, 'Giovanni Boccaccio', 0);

-- Question 3
INSERT INTO `questions` (`category_id`, `question_text`) SELECT id, 'Qual è la capitale della Francia?' FROM `categories` WHERE `name` = 'Cultura';
SET @last_question_id = LAST_INSERT_ID();
INSERT INTO `answers` (`question_id`, `answer_text`, `is_correct`) VALUES
(@last_question_id, 'Berlino', 0),
(@last_question_id, 'Madrid', 0),
(@last_question_id, 'Londra', 0),
(@last_question_id, 'Parigi', 1);
