-- --------------------------------------------------------
-- Server OS:                    Win64
-- HeidiSQL Version:             12.6.0.6765
-- --------------------------------------------------------



-- Dumping database structure for db_funkc
CREATE DATABASE IF NOT EXISTS `db_funkc` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `db_funkc`;

-- Dumping structure for function db_funkc.check_user_fn
DELIMITER //
CREATE FUNCTION `check_user_fn`(p_username VARCHAR(50), p_password_hash VARCHAR(255)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
    DECLARE stored_hash VARCHAR(255);
    SELECT password_hash INTO stored_hash FROM users WHERE username = p_username;

    IF stored_hash = p_password_hash THEN
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;
END//
DELIMITER ;

-- Dumping structure for function db_funkc.create_user_fn
DELIMITER //
CREATE FUNCTION `create_user_fn`(p_username VARCHAR(50), p_password_hash VARCHAR(255)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
    DECLARE existing INT DEFAULT 0;
    SELECT COUNT(*) INTO existing FROM users WHERE username = p_username;

    IF existing = 0 THEN
        INSERT INTO users(username, password_hash) VALUES (p_username, p_password_hash);
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;
END//
DELIMITER ;

-- Dumping structure for function db_funkc.delete_user_fn
DELIMITER //
CREATE FUNCTION `delete_user_fn`(p_username VARCHAR(50)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
    IF EXISTS(SELECT 1 FROM users WHERE username = p_username) THEN
        DELETE FROM users WHERE username = p_username;
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;
END//
DELIMITER ;

-- Dumping structure for function db_funkc.is_logged_in_fn
DELIMITER //
CREATE FUNCTION `is_logged_in_fn`(p_token VARCHAR(255)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
    IF EXISTS(SELECT 1 FROM sessions WHERE token = p_token AND expires_at > NOW()) THEN
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;
END//
DELIMITER ;

-- Dumping structure for function db_funkc.modify_user_fn
DELIMITER //
CREATE FUNCTION `modify_user_fn`(p_username VARCHAR(50), p_new_password_hash VARCHAR(255)) RETURNS tinyint(1)
    DETERMINISTIC
BEGIN
    IF EXISTS(SELECT 1 FROM users WHERE username = p_username) THEN
        UPDATE users SET password_hash = p_new_password_hash WHERE username = p_username;
        RETURN TRUE;
    ELSE
        RETURN FALSE;
    END IF;
END//
DELIMITER ;

-- Dumping structure for table db_funkc.sessions
CREATE TABLE IF NOT EXISTS `sessions` (
  `session_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `token` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `expires_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`session_id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `sessions_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Dumping data for table db_funkc.sessions: ~0 rows (approximately)
INSERT INTO `sessions` (`session_id`, `user_id`, `token`, `created_at`, `expires_at`) VALUES
	(3, 1, '2719ed15d9ab9c3d4363dc097b36f224454ef5811469c05e500b95b26fea1a42', '2026-01-27 14:44:50', '2026-01-27 15:14:50');

-- Dumping structure for table db_funkc.users
CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Dumping data for table db_funkc.users: ~2 rows (approximately)
INSERT INTO `users` (`user_id`, `username`, `password_hash`, `created_at`) VALUES
	(1, 'test', '$2y$10$JEWEYlMRnOpou3sOw1nufOw6Myy0TVImQan0zrQAT6FqviSdL3JV.', '2026-01-27 14:29:32'),
	(2, 'monkey', '$2y$10$ZK3sIml15CH8upTFQWBmLeTxzDyPi6PLYjyAKNuMeXZUWHaQHkXE.', '2026-01-27 14:30:48');

