
drop table categories cascade constraints;
drop table transactions cascade constraints;

--stores the clients categories with their info--
CREATE TABLE categories (
    category_id INT PRIMARY KEY,
    user_id INT NOT NULL,
    category_name VARCHAR2(100) NOT NULL,
    amount DECIMAL(10, 2),
    UNIQUE(user_id, category_name),
    FOREIGN KEY (user_id) REFERENCES users(user_id)
);


--stores all the clients transactions--
CREATE TABLE transactions (
    transaction_id int primary key,
    user_id int not null ,
    category_name VARCHAR2(100) NOT NULL,
    category_id int not null,
    expenses DECIMAL(10, 2) NOT NULL,
    descriptions VARCHAR2(255) default("non Précisé"),
    expense_date DATE NOT NULL,
    check (expenses > 0),
    foreign key (user_id) references users(user_id),
    foreign key (category_id) references categories(category_id)
    foreign key (category_name) references categories(category_name)
);

--sequences for our ids values--
drop sequence seq_client_id;
drop sequence seq_categories_id;
drop sequence seq_transaction_id;

create sequence seq_client_id start with 455;
create sequence seq_categories_id start with 1;
create sequence seq_transaction_id start with 1;
END;
END;
    COMMIT;
END add_category;
END;
END;
END;
END;
END;
END;
    RETURN v_balance;
END;
END update_balance;
    v_new_balance users.balance%TYPE;
BEGIN
    -- Ajouter la transaction
    INSERT INTO transactions (transaction_id, user_id, category_name, expenses, descriptions, expense_date)
    VALUES (seq_transaction_id.NEXTVAL, p_user_id, p_category_name, p_expenses, p_description, SYSDATE);
    
    -- Récupérer l'ID de la catégorie
    SELECT category_id INTO v_category_id
    FROM categories
    WHERE category_name = p_category_name AND user_id = p_user_id;
    
    -- Mettre à jour le montant de la catégorie
    UPDATE categories
    SET amount = amount + p_expenses
    WHERE category_id = v_category_id;
    
    v_new_balance := v_new_balance - p_expenses;

END;