-- Add core columns to items table for efficient querying

-- Item classification
ALTER TABLE items ADD COLUMN item_type TEXT NOT NULL DEFAULT 'resource';
ALTER TABLE items ADD COLUMN level INTEGER NOT NULL DEFAULT 1;

-- Economy
ALTER TABLE items ADD COLUMN price INTEGER NOT NULL DEFAULT 0;

-- Stacking and storage
ALTER TABLE items ADD COLUMN max_stack INTEGER NOT NULL DEFAULT 1;
ALTER TABLE items ADD COLUMN storage_size INTEGER NOT NULL DEFAULT 0;

-- Skills
ALTER TABLE items ADD COLUMN skill TEXT NOT NULL DEFAULT 'none';
ALTER TABLE items ADD COLUMN tool TEXT NOT NULL DEFAULT 'none';

-- Visual/UI
ALTER TABLE items ADD COLUMN description TEXT NOT NULL DEFAULT '';

-- Boolean flags (stored as INTEGER: 0=false, 1=true)
ALTER TABLE items ADD COLUMN two_handed INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN undroppable INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN undroppable_on_death INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN unequip_destroy INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN generate_icon INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN hide_milestone INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN cannot_craft_exceptional INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN storage_all_items INTEGER NOT NULL DEFAULT 0;

-- Ability and item IDs
ALTER TABLE items ADD COLUMN ability_id INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN special_ability INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN learn_ability_id INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN book_id INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN swap_item INTEGER NOT NULL DEFAULT 0;

-- Create indexes for commonly queried columns
CREATE INDEX idx_items_type ON items(item_type);
CREATE INDEX idx_items_level ON items(level);
CREATE INDEX idx_items_price ON items(price);
CREATE INDEX idx_items_skill ON items(skill);

-- Crafting recipes table
CREATE TABLE crafting_recipes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    product_item_id INTEGER NOT NULL,
    skill TEXT NOT NULL,
    level INTEGER NOT NULL,
    workbench_id INTEGER NOT NULL,
    xp INTEGER NOT NULL DEFAULT 0,
    unlocked_by_default INTEGER NOT NULL DEFAULT 1,
    checks TEXT, -- nullable, for conditional recipes
    FOREIGN KEY (product_item_id) REFERENCES items(id) ON DELETE CASCADE
);

CREATE INDEX idx_crafting_recipes_product ON crafting_recipes(product_item_id);
CREATE INDEX idx_crafting_recipes_skill ON crafting_recipes(skill);
CREATE INDEX idx_crafting_recipes_level ON crafting_recipes(level);
CREATE INDEX idx_crafting_recipes_workbench ON crafting_recipes(workbench_id);

-- Crafting recipe ingredients (many-to-many)
CREATE TABLE crafting_recipe_items (
    recipe_id INTEGER NOT NULL,
    item_id INTEGER NOT NULL,
    amount INTEGER NOT NULL,
    PRIMARY KEY (recipe_id, item_id),
    FOREIGN KEY (recipe_id) REFERENCES crafting_recipes(id) ON DELETE CASCADE,
    FOREIGN KEY (item_id) REFERENCES items(id)
);

CREATE INDEX idx_crafting_recipe_items_item ON crafting_recipe_items(item_id);
