-- Marketing System Database Tables
-- Run this script to create the necessary tables for the marketing file management system
-- 
-- Usage: Import this file into your MySQL database or run the commands individually
-- Make sure to select the correct database before running these commands

-- Disable foreign key checks temporarily to avoid constraint errors
SET FOREIGN_KEY_CHECKS = 0;

-- Create marketing_folders table
CREATE TABLE IF NOT EXISTS `marketing_folders` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `folder_name` varchar(255) NOT NULL,
    `parent_id` int(11) DEFAULT NULL,
    `description` text DEFAULT NULL,
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdby` varchar(100) DEFAULT NULL,
    `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    `updatedby` varchar(100) DEFAULT NULL,
    `accounthierarchy` text DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `parent_id` (`parent_id`),
    KEY `accounthierarchy_idx` (`accounthierarchy`(100))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create marketing_files table
CREATE TABLE IF NOT EXISTS `marketing_files` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(255) NOT NULL,
    `original_filename` varchar(255) NOT NULL,
    `file_path` varchar(500) NOT NULL,
    `thumbnail_path` varchar(500) DEFAULT NULL,
    `file_type` varchar(10) NOT NULL,
    `file_size` bigint(20) NOT NULL DEFAULT 0,
    `folder_id` int(11) DEFAULT NULL,
    `tags` json DEFAULT NULL,
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdby` varchar(100) DEFAULT NULL,
    `updated` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    `updatedby` varchar(100) DEFAULT NULL,
    `accounthierarchy` text DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `folder_id` (`folder_id`),
    KEY `file_type` (`file_type`),
    KEY `accounthierarchy_idx` (`accounthierarchy`(100)),
    KEY `created_idx` (`created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create marketing_tags table
CREATE TABLE IF NOT EXISTS `marketing_tags` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `tag_name` varchar(100) NOT NULL,
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `tag_name` (`tag_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create marketing_file_tags junction table
CREATE TABLE IF NOT EXISTS `marketing_file_tags` (
    `file_id` int(11) NOT NULL,
    `tag_id` int(11) NOT NULL,
    PRIMARY KEY (`file_id`, `tag_id`),
    KEY `file_id` (`file_id`),
    KEY `tag_id` (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Add foreign key constraints after all tables are created
ALTER TABLE `marketing_folders` 
ADD CONSTRAINT `fk_marketing_folders_parent` 
FOREIGN KEY (`parent_id`) REFERENCES `marketing_folders`(`id`) ON DELETE CASCADE;

ALTER TABLE `marketing_files` 
ADD CONSTRAINT `fk_marketing_files_folder` 
FOREIGN KEY (`folder_id`) REFERENCES `marketing_folders`(`id`) ON DELETE SET NULL;

ALTER TABLE `marketing_file_tags` 
ADD CONSTRAINT `fk_marketing_file_tags_file` 
FOREIGN KEY (`file_id`) REFERENCES `marketing_files`(`id`) ON DELETE CASCADE;

ALTER TABLE `marketing_file_tags` 
ADD CONSTRAINT `fk_marketing_file_tags_tag` 
FOREIGN KEY (`tag_id`) REFERENCES `marketing_tags`(`id`) ON DELETE CASCADE;

-- Re-enable foreign key checks
SET FOREIGN_KEY_CHECKS = 1;

-- Insert some default sample data (optional)
-- Uncomment the lines below if you want to start with sample folders and tags

-- INSERT INTO `marketing_folders` (`folder_name`, `description`, `createdby`) VALUES
-- ('Product Brochures', 'Marketing brochures and product information', 'system'),
-- ('Technical Specifications', 'Technical documentation and specifications', 'system'),
-- ('Images', 'Product images and photos', 'system'),
-- ('Videos', 'Product videos and demonstrations', 'system');

-- INSERT INTO `marketing_tags` (`tag_name`) VALUES
-- ('brochure'),
-- ('specification'),
-- ('manual'),
-- ('image'),
-- ('video'),
-- ('product'),
-- ('marketing'),
-- ('technical');

-- Create upload directories (Note: This requires manual creation on file system)
-- Create the following directories in your web server:
-- - ./marketing/uploads/
-- - ./marketing/uploads/thumbs/
-- 
-- Linux/macOS commands:
-- mkdir -p marketing/uploads/thumbs
-- chmod 755 marketing/uploads
-- chmod 755 marketing/uploads/thumbs