-- Shift Management Tables
CREATE TABLE `admin_shifts` (
`id` int AUTO_INCREMENT NOT NULL,
`name` varchar(100) NOT NULL,
`startTime` varchar(5) NOT NULL,
`endTime` varchar(5) NOT NULL,
`shiftDate` date NOT NULL,
`requiredCount` int NOT NULL DEFAULT 1,
`status` enum('open','filled','overfilled') NOT NULL DEFAULT 'open',
`createdBy` int NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT `admin_shifts_id` PRIMARY KEY (`id`),
INDEX `idx_admin_shifts_date` (`shiftDate`),
INDEX `idx_admin_shifts_status` (`status`)
);

CREATE TABLE `admin_shift_assignments` (
`id` int AUTO_INCREMENT NOT NULL,
`shiftId` int NOT NULL,
`adminId` int NOT NULL,
`assignedBy` int NOT NULL,
`status` enum('assigned','confirmed','completed','no_show') NOT NULL DEFAULT 'assigned',
`assignedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`confirmedAt` timestamp,
`completedAt` timestamp,
CONSTRAINT `admin_shift_assignments_id` PRIMARY KEY (`id`),
INDEX `idx_shift_assignments_shiftId` (`shiftId`),
INDEX `idx_shift_assignments_adminId` (`adminId`)
);

CREATE TABLE `admin_shift_swaps` (
`id` int AUTO_INCREMENT NOT NULL,
`requestingAdminId` int NOT NULL,
`requestingWithAdminId` int NOT NULL,
`fromShiftId` int NOT NULL,
`toShiftId` int NOT NULL,
`status` enum('pending','approved','rejected','cancelled') NOT NULL DEFAULT 'pending',
`reason` text,
`responseNotes` text,
`requestedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`respondedAt` timestamp,
CONSTRAINT `admin_shift_swaps_id` PRIMARY KEY (`id`),
INDEX `idx_shift_swaps_requestingAdminId` (`requestingAdminId`),
INDEX `idx_shift_swaps_status` (`status`)
);

-- Knowledge Base Tables
CREATE TABLE `knowledge_articles` (
`id` int AUTO_INCREMENT NOT NULL,
`title` varchar(255) NOT NULL,
`category` varchar(100) NOT NULL,
`content` longtext NOT NULL,
`tags` json,
`author` int NOT NULL,
`featured` tinyint NOT NULL DEFAULT 0,
`viewCount` int NOT NULL DEFAULT 0,
`helpfulCount` int NOT NULL DEFAULT 0,
`unhelpfulCount` int NOT NULL DEFAULT 0,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT `knowledge_articles_id` PRIMARY KEY (`id`),
INDEX `idx_knowledge_articles_category` (`category`),
INDEX `idx_knowledge_articles_featured` (`featured`),
INDEX `idx_knowledge_articles_author` (`author`)
);

CREATE TABLE `knowledge_article_votes` (
`id` int AUTO_INCREMENT NOT NULL,
`articleId` int NOT NULL,
`adminId` int NOT NULL,
`helpful` tinyint NOT NULL,
`votedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `knowledge_article_votes_id` PRIMARY KEY (`id`),
INDEX `idx_article_votes_articleId` (`articleId`),
INDEX `idx_article_votes_adminId` (`adminId`)
);

-- Delegation Tables
CREATE TABLE `admin_delegations` (
`id` int AUTO_INCREMENT NOT NULL,
`title` varchar(255) NOT NULL,
`description` text,
`assigneeId` int NOT NULL,
`delegatedBy` int NOT NULL,
`priority` enum('low','medium','high','critical') NOT NULL DEFAULT 'medium',
`category` varchar(100),
`status` enum('pending','in-progress','completed','cancelled') NOT NULL DEFAULT 'pending',
`progress` int NOT NULL DEFAULT 0,
`dueDate` date NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`completedAt` timestamp,
`updatedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT `admin_delegations_id` PRIMARY KEY (`id`),
INDEX `idx_delegations_assigneeId` (`assigneeId`),
INDEX `idx_delegations_status` (`status`),
INDEX `idx_delegations_dueDate` (`dueDate`)
);

CREATE TABLE `admin_delegation_notes` (
`id` int AUTO_INCREMENT NOT NULL,
`delegationId` int NOT NULL,
`authorId` int NOT NULL,
`content` text NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `admin_delegation_notes_id` PRIMARY KEY (`id`),
INDEX `idx_delegation_notes_delegationId` (`delegationId`)
);

CREATE TABLE `admin_delegation_approvals` (
`id` int AUTO_INCREMENT NOT NULL,
`delegationId` int NOT NULL,
`approverId` int NOT NULL,
`status` enum('pending','approved','rejected') NOT NULL DEFAULT 'pending',
`comments` text,
`approvedAt` timestamp,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT `admin_delegation_approvals_id` PRIMARY KEY (`id`),
INDEX `idx_delegation_approvals_delegationId` (`delegationId`),
INDEX `idx_delegation_approvals_approverId` (`approverId`)
);
