-- Create database if it doesn't exist
CREATE DATABASE IF NOT EXISTS uavlicense;

USE uavlicense;

-- Drop existing tables if they exist
DROP TABLE IF EXISTS licenses;
DROP TABLE IF EXISTS users;

-- Create users table
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    full_name VARCHAR(255) NOT NULL,
    email VARCHAR(255),
    role ENUM('admin', 'user') DEFAULT 'user',
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Create licenses table
CREATE TABLE licenses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    uniqid VARCHAR(100) NOT NULL UNIQUE,
    code VARCHAR(100),
    organization VARCHAR(255),
    application_date DATE,
    bpla_info VARCHAR(255),
    flight_start TIME,
    flight_end TIME,
    region VARCHAR(255),
    radius_zone INT,
    coordinates TEXT,
    height_zone INT,
    flight_altitude INT,
    job_type VARCHAR(255),
    operator VARCHAR(255),
    phone_number VARCHAR(255),
    application_number INT,
    job_date DATE,
    is_valid BOOLEAN DEFAULT TRUE,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id)
);