-- ====================================================================
-- DATABASE ARCHITECTURE CONFIGURATION FOR DAB ENTERPRISE LTD
-- NATIONAL PRACTICAL EXAMINATION MATRIX PRODUCTION LAYOUT
-- ====================================================================

CREATE DATABASE IF NOT EXISTS dab_enterprise_db;
USE dab_enterprise_db;

-- --------------------------------------------------------------------
-- 1. AUTHENTICATION & IDENTITY SERVICES ENTITY
-- Tracks user system accounts for secure dashboard login credentials.
-- --------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL
);

-- --------------------------------------------------------------------
-- 2. PRODUCTS MASTER ENTITY TABLE (Exact Page 1 Exam Blueprint)
-- Manages core product records, categories, pricing, and automated 
-- total value computations.
-- --------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS products (
    product_id INT AUTO_INCREMENT PRIMARY KEY,
    ProductName VARCHAR(255) NOT NULL,
    Category VARCHAR(255) NOT NULL,
    Quantity INT NOT NULL DEFAULT 0,
    UnitPrice DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
    -- ITEM Requirement: TotalPrice automatically computed via database engine metrics
    TotalPrice DECIMAL(10, 2) GENERATED ALWAYS AS (Quantity * UnitPrice) STORED
);

-- --------------------------------------------------------------------
-- 3. SALES TRANSACTION ENTITY TABLE
-- Records every external business sell-out transaction ledger line, 
-- dynamically factoring total revenue performance numbers.
-- --------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS sales (
    sales_id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT NOT NULL,
    SoldQuantity INT NOT NULL DEFAULT 0,
    SoldUnitPrice DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
    -- ITEM Requirement: SoldTotalPrice automatically tracked as quantity * unit price
    SoldTotalPrice DECIMAL(10, 2) GENERATED ALWAYS AS (SoldQuantity * SoldUnitPrice) STORED,
    SalesDate DATE NOT NULL,
    FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE
);

-- --------------------------------------------------------------------
-- 4. STOCK STATUS INVENTORY TRACKING ENTITY (Last Form Full CRUD Table)
-- Monitors warehouse volume streams. Automatically computes remaining 
-- safe capacity margins to protect against under-stock risks.
-- --------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS stock_status (
    stock_id INT AUTO_INCREMENT PRIMARY KEY,
    product_id INT NOT NULL UNIQUE,
    AvailableQuantity INT NOT NULL DEFAULT 0,
    SoldQuantity INT NOT NULL DEFAULT 0,
    -- ITEM Requirement: RemainingQuantity computed from storage levels and outflows
    RemainingQuantity INT GENERATED ALWAYS AS (AvailableQuantity - SoldQuantity) STORED,
    FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE
);

-- ====================================================================
-- SEED INITIAL DATA PATCH (OPTIONAL TESTING LOGS)
-- Use this block to verify variable mapping functionality inside views.
-- ====================================================================
-- INSERT INTO users (username, password) VALUES ('admin', '$2b$10$UnHashedBcryptPlaceholderValues');