-- =======================================================================================
-- ENTERPRISE PROPERTY MANAGEMENT & APARTMENT SELLING DATABASE SCHEMA v2.0
-- =======================================================================================
-- Database: property_system
-- Author: AI Agent
-- Date: 2026-01-20
-- Description: Detailed, normalized schema for high-end property management.
--              Includes CRM, Audit Trails, Comprehensive Leases, and Financials.
-- =======================================================================================

SET FOREIGN_KEY_CHECKS = 0;

-- =======================================================================================
-- 1. AUTHENTICATION, USERS & PROFILES
-- =======================================================================================

-- Users Table: Core identity with extended personal details
CREATE TABLE IF NOT EXISTS `users` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `role_id` INT NOT NULL,
    `first_name` VARCHAR(100) NOT NULL,
    `middle_name` VARCHAR(100),
    `last_name` VARCHAR(100) NOT NULL,
    `email` VARCHAR(150) NOT NULL UNIQUE,
    `phone` VARCHAR(20) NOT NULL UNIQUE,
    `phone_secondary` VARCHAR(20),
    `gender` ENUM('Male', 'Female', 'Other', 'Prefer Not to Say'),
    `dob` DATE,
    `nationality` VARCHAR(100),
    `id_type` ENUM('National ID', 'Passport', 'Alien ID') DEFAULT 'National ID',
    `id_number` VARCHAR(50) UNIQUE,
    `password_hash` VARCHAR(255) NOT NULL,
    `profile_photo_url` VARCHAR(255),
    `address_postal` VARCHAR(255),
    `city` VARCHAR(100),
    `country` VARCHAR(100) DEFAULT 'Kenya',
    `status` ENUM('active', 'inactive', 'banned', 'pending_verification') DEFAULT 'pending_verification',
    `two_factor_enabled` BOOLEAN DEFAULT FALSE,
    `last_login` DATETIME,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`)
) ENGINE=InnoDB;

-- Roles & Permissions (Standard RBAC)
CREATE TABLE IF NOT EXISTS `roles` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `role_name` VARCHAR(50) NOT NULL UNIQUE,
    `description` TEXT
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `permissions` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `permission_name` VARCHAR(100) NOT NULL UNIQUE,
    `group_name` VARCHAR(50) -- e.g., 'Finance', 'Property'
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `role_permissions` (
    `role_id` INT NOT NULL,
    `permission_id` INT NOT NULL,
    PRIMARY KEY (`role_id`, `permission_id`),
    FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

-- =======================================================================================
-- 2. DETAILED PROPERTY MANAGEMENT
-- =======================================================================================

-- Properties: Extended metadata
CREATE TABLE IF NOT EXISTS `properties` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `owner_id` INT NOT NULL, -- The Landlord
    `manager_id` INT, -- Assigned Property Manager Agent
    `name` VARCHAR(200) NOT NULL,
    `type` ENUM('Apartment Complex', 'Commercial Tower', 'Gated Community', 'Mall', 'Single Unit', 'Land') NOT NULL,
    
    -- Location Details
    `location_area` VARCHAR(100) NOT NULL, -- e.g., Kilimani
    `street_address` VARCHAR(255),
    `city` VARCHAR(100),
    `geo_lat` DECIMAL(10, 8),
    `geo_lng` DECIMAL(11, 8),
    `l_r_number` VARCHAR(100), -- Land Reference Number
    
    -- Physical Details
    `land_size` VARCHAR(50), -- e.g. '0.5 Acres'
    `total_floors` INT DEFAULT 1,
    `total_units` INT DEFAULT 0,
    `construction_year` YEAR,
    `description` TEXT,
    
    -- Financial Configuration
    `management_fee_percentage` DECIMAL(5,2) DEFAULT 0.00,
    `vat_enabled` BOOLEAN DEFAULT FALSE,
    
    -- Media
    `cover_image_url` VARCHAR(255),
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`manager_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- Units: Granular specs
CREATE TABLE IF NOT EXISTS `units` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `property_id` INT NOT NULL,
    `unit_type_id` INT, -- Linked to `unit_types`
    `unit_number` VARCHAR(50) NOT NULL,
    `floor_level` INT,
    
    -- Specs
    `size_sqm` DECIMAL(10, 2),
    `bedrooms` INT DEFAULT 0,
    `bathrooms` INT DEFAULT 0,
    `balconies` INT DEFAULT 0,
    `furnishing` ENUM('Unfurnished', 'Semi-Furnished', 'Furnished') DEFAULT 'Unfurnished',
    `has_parking` BOOLEAN DEFAULT FALSE,
    `parking_slot_no` VARCHAR(50),
    
    -- Utilities
    `electricity_meter_no` VARCHAR(50),
    `water_meter_no` VARCHAR(50),
    
    -- Financials
    `rent_amount` DECIMAL(15, 2) NOT NULL,
    `deposit_amount` DECIMAL(15, 2) DEFAULT 0.00,
    `sale_price` DECIMAL(15, 2) DEFAULT NULL,
    
    -- Status
    `status` ENUM('vacant', 'occupied', 'maintained', 'reserved', 'sold') DEFAULT 'vacant',
    `is_for_sale` BOOLEAN DEFAULT FALSE,
    `notes` TEXT,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`property_id`) REFERENCES `properties`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

-- Amenities & Types (Kept simple but expanded)
CREATE TABLE IF NOT EXISTS `unit_types` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(100) NOT NULL, -- 1BR, 2BR, Penthouse
    `base_rent` DECIMAL(15, 2)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `amenities` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(100) UNIQUE,
    `icon_class` VARCHAR(50) -- For UI
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `property_amenities` (
    `property_id` INT NOT NULL,
    `amenity_id` INT NOT NULL,
    PRIMARY KEY (`property_id`, `amenity_id`)
) ENGINE=InnoDB;

-- =======================================================================================
-- 3. ADVANCED LEASE & TENANT MANAGEMENT
-- =======================================================================================

-- Active Leases: Complete contract details
CREATE TABLE IF NOT EXISTS `leases` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `unit_id` INT NOT NULL,
    `tenant_id` INT NOT NULL,
    
    -- Dates
    `start_date` DATE NOT NULL,
    `end_date` DATE NOT NULL,
    `move_in_date` DATE,
    `termination_date` DATE,
    
    -- Financial Terms
    `rent_amount` DECIMAL(15, 2) NOT NULL,
    `deposit_amount` DECIMAL(15, 2) NOT NULL,
    `service_charge` DECIMAL(15, 2) DEFAULT 0.00,
    `water_deposit` DECIMAL(15, 2) DEFAULT 0.00,
    `electricity_deposit` DECIMAL(15, 2) DEFAULT 0.00,
    `payment_frequency` ENUM('monthly', 'quarterly', 'yearly') DEFAULT 'monthly',
    `due_day` INT DEFAULT 5, 
    
    -- Rules
    `grace_period_days` INT DEFAULT 5,
    `late_fee_type` ENUM('Fixed', 'Percentage') DEFAULT 'Fixed',
    `late_fee_value` DECIMAL(15, 2) DEFAULT 0.00,
    `escalation_rate_percent` DECIMAL(5,2) DEFAULT 0.00, -- Yearly rent increase
    
    -- Status
    `status` ENUM('active', 'terminated', 'expired', 'eviction_pending') DEFAULT 'active',
    `agreement_doc_url` VARCHAR(255),
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`),
    FOREIGN KEY (`tenant_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- Tenant Extended Profile (KYC)
CREATE TABLE IF NOT EXISTS `tenant_kyc` (
    `user_id` INT PRIMARY KEY,
    `kra_pin` VARCHAR(50),
    `occupation` VARCHAR(100),
    `employer_name` VARCHAR(100),
    `employer_phone` VARCHAR(50),
    `employer_address` VARCHAR(255),
    `monthly_income_range` VARCHAR(100),
    
    -- Next of Kin / Emergency
    `nok_name` VARCHAR(100),
    `nok_relation` VARCHAR(50),
    `nok_phone` VARCHAR(50),
    
    -- Preferences
    `has_pets` BOOLEAN DEFAULT FALSE,
    `pet_details` VARCHAR(255),
    `car_reg_no` VARCHAR(50),
    
    -- Verification
    `id_copy_url` VARCHAR(255),
    `payslip_url` VARCHAR(255),
    `is_verified` BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- =======================================================================================
-- 4. SALES CRM & PIPELINE
-- =======================================================================================

-- Leads: CRM functionality
CREATE TABLE IF NOT EXISTS `leads` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `assigned_agent_id` INT,
    `first_name` VARCHAR(100),
    `last_name` VARCHAR(100),
    `email` VARCHAR(150),
    `phone` VARCHAR(50),
    
    -- Interest
    `interest_type` ENUM('Rent', 'Buy') DEFAULT 'Rent',
    `property_interested_in` INT, -- Optional link to property
    `budget_min` DECIMAL(15, 2),
    `budget_max` DECIMAL(15, 2),
    `preferred_location` VARCHAR(100),
    
    -- Status
    `source` VARCHAR(50), -- Facebook, Walk-in, Website
    `status` ENUM('New', 'Contacted', 'Viewing Scheduled', 'Offer Made', 'Converted', 'Lost') DEFAULT 'New',
    `notes` TEXT,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`assigned_agent_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- Unit Sales: The Transaction
CREATE TABLE IF NOT EXISTS `unit_sales` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `unit_id` INT NOT NULL,
    `buyer_id` INT NOT NULL,
    `agent_id` INT, 
    
    -- Pricing
    `agreed_price` DECIMAL(15, 2) NOT NULL,
    `reservation_fee` DECIMAL(15, 2) DEFAULT 0.00,
    `stamp_duty` DECIMAL(15, 2) DEFAULT 0.00,
    `legal_fees` DECIMAL(15, 2) DEFAULT 0.00,
    `vat_amount` DECIMAL(15, 2) DEFAULT 0.00,
    `total_payable` DECIMAL(15, 2) NOT NULL,
    
    -- Legal
    `offer_letter_date` DATE,
    `sale_agreement_date` DATE,
    `completion_date` DATE,
    `lawyer_name` VARCHAR(100),
    `lawyer_contact` VARCHAR(50),
    
    -- Status
    `status` ENUM('draft', 'reserved', 'documentation', 'payment_processing', 'completed', 'cancelled') DEFAULT 'draft',
    `documents_folder_id` VARCHAR(100),
    FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`),
    FOREIGN KEY (`buyer_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- Sales Payments (Installments)
CREATE TABLE IF NOT EXISTS `sale_installments` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `sale_id` INT NOT NULL,
    `milestone_name` VARCHAR(100), -- e.g. "Booking Fee", "Foundation", "Roofing"
    `due_date` DATE NOT NULL,
    `amount` DECIMAL(15, 2) NOT NULL,
    `penalty_rate` DECIMAL(5,2) DEFAULT 0.00,
    `status` ENUM('pending', 'paid', 'overdue') DEFAULT 'pending',
    `paid_date` DATETIME,
    `receipt_number` VARCHAR(100),
    FOREIGN KEY (`sale_id`) REFERENCES `unit_sales`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;

-- =======================================================================================
-- 5. ADVANCED FINANCE
-- =======================================================================================

CREATE TABLE IF NOT EXISTS `invoices` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `invoice_number` VARCHAR(50) NOT NULL UNIQUE,
    `lease_id` INT DEFAULT NULL,
    `tenant_id` INT NOT NULL,
    `property_id` INT, -- Useful for global filtering
    
    `type` ENUM('Rent', 'Water', 'Electricity', 'Service Charge', 'Maintenance', 'Penalty', 'Sale Installment', 'Deposit') NOT NULL,
    `description` VARCHAR(255),
    
    `amount` DECIMAL(15, 2) NOT NULL,
    `tax_amount` DECIMAL(15, 2) DEFAULT 0.00,
    `discount_amount` DECIMAL(15, 2) DEFAULT 0.00,
    `total_amount` DECIMAL(15, 2) NOT NULL, -- (Amount + Tax - Discount)
    
    `paid_amount` DECIMAL(15, 2) DEFAULT 0.00,
    `balance` DECIMAL(15, 2) NOT NULL,
    `due_date` DATE NOT NULL,
    `generated_date` DATE DEFAULT (CURRENT_DATE),
    `status` ENUM('unpaid', 'partial', 'paid', 'overdue', 'void') DEFAULT 'unpaid',
    
    `is_recurring` BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (`lease_id`) REFERENCES `leases`(`id`),
    FOREIGN KEY (`tenant_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `payments` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `invoice_id` INT, -- Can be null for 'On Account' payments
    `payer_id` INT NOT NULL,
    
    `amount` DECIMAL(15, 2) NOT NULL,
    `payment_method` ENUM('M-Pesa', 'Bank Transfer', 'Cheque', 'Cash', 'Credit Card', 'Wallet Balance') NOT NULL,
    `transaction_reference` VARCHAR(100) NOT NULL UNIQUE, -- e.g. M-Pesa Code
    `transaction_date` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `payment_proof_url` VARCHAR(255),
    
    `status` ENUM('pending', 'verified', 'bounced', 'refunded') DEFAULT 'verified',
    `verified_by` INT, -- Admin user ID
    FOREIGN KEY (`invoice_id`) REFERENCES `invoices`(`id`),
    FOREIGN KEY (`payer_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

-- Expenses linked to Vendors
CREATE TABLE IF NOT EXISTS `expenses` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `property_id` INT NOT NULL,
    `vendor_id` INT, -- If applicable
    
    `category` VARCHAR(100) NOT NULL, -- e.g. Utilities, Repair, Salary, Legal, Marketing
    `description` TEXT,
    `amount` DECIMAL(15, 2) NOT NULL,
    `tax_deductible` BOOLEAN DEFAULT TRUE,
    
    `incurred_date` DATE NOT NULL,
    `payment_method` VARCHAR(50),
    `reference_no` VARCHAR(100),
    `receipt_doc_url` VARCHAR(255),
    
    `status` ENUM('pending_approval', 'approved', 'paid') DEFAULT 'pending_approval',
    `recorded_by` INT,
    FOREIGN KEY (`property_id`) REFERENCES `properties`(`id`)
) ENGINE=InnoDB;

-- =======================================================================================
-- 6. MAINTENANCE & VENDORS
-- =======================================================================================

CREATE TABLE IF NOT EXISTS `vendors` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `company_name` VARCHAR(150) NOT NULL,
    `contact_person` VARCHAR(100),
    `phone` VARCHAR(50),
    `email` VARCHAR(100),
    
    `service_category` VARCHAR(100), -- Plumbing, Security, Cleaning
    `hourly_rate` DECIMAL(10,2),
    `tax_pin` VARCHAR(50),
    `bank_details` TEXT,
    `rating` DECIMAL(3,2) DEFAULT 0.00,
    `status` ENUM('active', 'blacklisted') DEFAULT 'active'
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `maintenance_requests` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `ticket_number` VARCHAR(50) UNIQUE,
    `property_id` INT NOT NULL,
    `unit_id` INT,
    `requester_id` INT NOT NULL, -- Tenant
    
    `category` VARCHAR(50), -- Electrical, Plumbing
    `priority` ENUM('Low', 'Medium', 'High', 'Emergency') DEFAULT 'Medium',
    `subject` VARCHAR(150),
    `description` TEXT,
    `images_json` JSON, -- Array of image URLs
    
    `preferred_visit_date` DATE,
    `access_instructions` TEXT,
    
    `assigned_vendor_id` INT,
    `scheduled_date` DATETIME,
    `completed_date` DATETIME,
    `cost_estimate` DECIMAL(15,2),
    `final_cost` DECIMAL(15,2),
    
    `status` ENUM('Open', 'Assigned', 'In Progress', 'On Hold', 'Completed', 'Cancelled') DEFAULT 'Open',
    `admin_notes` TEXT,
    FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`),
    FOREIGN KEY (`requester_id`) REFERENCES `users`(`id`),
    FOREIGN KEY (`assigned_vendor_id`) REFERENCES `vendors`(`id`)
) ENGINE=InnoDB;

-- =======================================================================================
-- 7. COMMUNICATIONS & LOGS
-- =======================================================================================

CREATE TABLE IF NOT EXISTS `communications` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `type` ENUM('SMS', 'Email', 'Push') NOT NULL,
    `direction` ENUM('Outbound', 'Inbound') DEFAULT 'Outbound',
    `recipient` VARCHAR(150),
    `subject` VARCHAR(255),
    `message_body` TEXT,
    `status` ENUM('Queued', 'Sent', 'Delivered', 'Failed') DEFAULT 'Queued',
    `sent_at` DATETIME,
    `error_log` TEXT,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `activity_logs` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT,
    `action_type` VARCHAR(50), -- CREATE, UPDATE, DELETE, LOGIN
    `module` VARCHAR(50), -- LEASE, PROPERTY, INVOICE
    `reference_id` INT, -- ID of the entity
    `old_values` JSON,
    `new_values` JSON,
    `ip_address` VARCHAR(45),
    `user_agent` VARCHAR(255),
    `timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

-- =======================================================================================
-- 8. RESTORED TRIGGERS & PROCEDURES (ADJUSTED FOR NEW SCHEMA)
-- =======================================================================================

DELIMITER //

-- Trigger: Update Invoice Balance
CREATE TRIGGER `trg_after_payment_insert`
AFTER INSERT ON `payments`
FOR EACH ROW
BEGIN
    DECLARE v_balance DECIMAL(15, 2);
    
    IF NEW.invoice_id IS NOT NULL THEN
        -- Reduce invoice balance
        UPDATE `invoices` 
        SET `balance` = `balance` - NEW.amount, 
            `paid_amount` = `paid_amount` + NEW.amount
        WHERE `id` = NEW.invoice_id;
        
        -- Check status
        SELECT `balance` INTO v_balance FROM `invoices` WHERE `id` = NEW.invoice_id;
        
        IF v_balance <= 0 THEN
            UPDATE `invoices` SET `status` = 'paid', `balance` = 0 WHERE `id` = NEW.invoice_id;
        ELSE
            UPDATE `invoices` SET `status` = 'partial' WHERE `id` = NEW.invoice_id;
        END IF;

        -- Log
        INSERT INTO activity_logs (user_id, action_type, module, reference_id, new_values)
        VALUES (NEW.payer_id, 'PAYMENT', 'FINANCE', NEW.id, JSON_OBJECT('amount', NEW.amount, 'ref', NEW.transaction_reference));
    END IF;
END //

-- Trigger: Lease Unit Occupancy
CREATE TRIGGER `trg_after_lease_insert`
AFTER INSERT ON `leases`
FOR EACH ROW
BEGIN
    IF NEW.status = 'active' THEN
        UPDATE `units` SET `status` = 'occupied' WHERE `id` = NEW.unit_id;
    END IF;
END //

-- Procedure: Generate Rent (Adjusted for Invoice table)
CREATE PROCEDURE `sp_generate_monthly_invoices`()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE v_lease_id, v_tenant_id, v_prop_id INT;
    DECLARE v_rent DECIMAL(15,2);
    DECLARE v_due_day INT;
    DECLARE v_invoice_month VARCHAR(6);
    DECLARE v_due_date DATE;
    DECLARE v_exists INT;
    
    DECLARE leases_cursor CURSOR FOR 
        SELECT l.id, l.tenant_id, l.rent_amount, l.due_day, u.property_id 
        FROM leases l JOIN units u ON l.unit_id = u.id
        WHERE l.status = 'active';
        
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    SET v_invoice_month = DATE_FORMAT(NOW(), '%Y%m');
    
    OPEN leases_cursor;
    
    read_loop: LOOP
        FETCH leases_cursor INTO v_lease_id, v_tenant_id, v_rent_amount, v_due_day, v_prop_id;
        IF done THEN LEAVE read_loop; END IF;
        
        -- Check duplicates
        SELECT COUNT(*) INTO v_exists FROM invoices 
        WHERE lease_id = v_lease_id AND description LIKE CONCAT('Rent - %', v_invoice_month);
        
        IF v_exists = 0 THEN
            SET v_due_date = STR_TO_DATE(CONCAT(DATE_FORMAT(NOW(), '%Y-%m-'), v_due_day), '%Y-%m-%d');
            
            INSERT INTO invoices (
                invoice_number, lease_id, tenant_id, property_id, type, description, 
                amount, total_amount, balance, due_date, status
            ) VALUES (
                CONCAT('INV-', v_lease_id, '-', v_invoice_month), 
                v_lease_id, v_tenant_id, v_prop_id, 'Rent', 
                CONCAT('Rent - ', v_invoice_month), 
                v_rent_amount, v_rent_amount, v_rent_amount, 
                v_due_date, 'unpaid'
            );
        END IF;
    END LOOP;
    CLOSE leases_cursor;
END //

DELIMITER ;

SET FOREIGN_KEY_CHECKS = 1;

-- =======================================================================================
-- END SCHEMA v2.0
-- =======================================================================================
