
CREATE DATABASE IF NOT EXISTS smart_bins;
USE smart_bins;


CREATE TABLE bins (
    id INT AUTO_INCREMENT PRIMARY KEY,
    bin_label VARCHAR(50) NOT NULL,    
    latitude DECIMAL(10, 8),           
    longitude DECIMAL(11, 8),
    empty_weight_kg DECIMAL(5, 2),    
    max_capacity_liters INT,          
    date_installed TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE measurements (
    id INT AUTO_INCREMENT PRIMARY KEY,
    bin_id INT,
    total_weight_kg DECIMAL(5, 2),
    fill_level_percent INT,
    temperature DECIMAL(4, 2),
    battery_level INT,
    measured_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (bin_id) REFERENCES bins(id) ON DELETE CASCADE
);


CREATE USER IF NOT EXISTS 'web_user'@'%' IDENTIFIED BY 'Doupio_750985$';
GRANT ALL PRIVILEGES ON smart_bins.* TO 'web_user'@'%';
FLUSH PRIVILEGES;
