-- CreateTable Category
CREATE TABLE `Category` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `parentId` INTEGER NULL,
    PRIMARY KEY (`id`),
    UNIQUE INDEX `Category_name_parentId_key`(`name`, `parentId`),
    INDEX `Category_parentId_idx`(`parentId`),
    CONSTRAINT `Category_parentId_fkey` FOREIGN KEY (`parentId`) REFERENCES `Category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
);

-- Add categoryId to Product
ALTER TABLE `Product` ADD COLUMN `categoryId` INTEGER NULL;
ALTER TABLE `Product` ADD CONSTRAINT `Product_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `Category` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- ============================================================
-- Seed: Huvudkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Bröd & Kakor', NULL);
SET @brod_kakor = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Färdigmat', NULL);
SET @fardigmat = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Fryst', NULL);
SET @fryst = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Frukt & Grönt', NULL);
SET @frukt_gront = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Glass, godis & snacks', NULL);
SET @glass = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kött, chark & fågel', NULL);
SET @kott = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Mejeri, ost & ägg', NULL);
SET @mejeri = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Skafferi', NULL);
SET @skafferi = LAST_INSERT_ID();

-- ============================================================
-- Bröd & Kakor → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Bröd', @brod_kakor);
SET @brod = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kex & Kakor', @brod_kakor);
SET @kex = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Matbröd', @brod);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Matkex', @kex);

-- ============================================================
-- Färdigmat → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Såser, grytbaser & övriga smaksättare', @fardigmat);
SET @fd_saser = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Gratäng & Röror mm', @fardigmat);
SET @fd_grataeng = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Dressing & övriga smaksättare', @fd_saser);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Krämiga sallader', @fd_grataeng);

-- ============================================================
-- Fryst → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Bageri', @fryst);
SET @fr_bageri = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Grönsaker & kryddor', @fryst);
SET @fr_gron = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Bröd & fikabröd', @fr_bageri);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kryddor', @fr_gron);

-- ============================================================
-- Frukt & Grönt → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Potatis & rotsaker', @frukt_gront);
SET @fg_potatis = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kryddor & smaksättare', @frukt_gront);
SET @fg_kryddor = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Lök', @fg_potatis);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Smaksättare', @fg_kryddor);

-- ============================================================
-- Glass, godis & snacks → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Chips, snacks & dip', @glass);
SET @gl_chips = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Chips', @gl_chips);

-- ============================================================
-- Kött, chark & fågel → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Pålägg', @kott);
SET @ko_paalagg = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Korv', @kott);
SET @ko_korv = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Skivat pålägg', @ko_paalagg);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Ölkorv', @ko_korv);

-- ============================================================
-- Mejeri, ost & ägg → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Ost', @mejeri);
SET @me_ost = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Mjölk', @mejeri);
SET @me_mjolk = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Filmjölk & Yoghurt', @mejeri);
SET @me_filmjolk = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Färskost', @me_ost);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Dessertost', @me_ost);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Standardmjölk', @me_mjolk);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Filmjölk', @me_filmjolk);

-- ============================================================
-- Skafferi → underkategorier
-- ============================================================
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kryddor & smaksättare', @skafferi);
SET @sk_kryddor = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Pasta, ris & matgryn', @skafferi);
SET @sk_pasta = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Oliver & delikatesser', @skafferi);
SET @sk_oliver = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Konserver & burkar', @skafferi);
SET @sk_konserver = LAST_INSERT_ID();
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Asien', @skafferi);
SET @sk_asien = LAST_INSERT_ID();

INSERT INTO `Category` (`name`, `parentId`) VALUES ('Sås, dressing & majonnäs', @sk_kryddor);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Kryddor', @sk_kryddor);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Färsk pasta', @sk_pasta);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Delikatesser', @sk_oliver);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Tomatkonserver', @sk_konserver);
INSERT INTO `Category` (`name`, `parentId`) VALUES ('Såser & grytbaser', @sk_asien);
