CREATE DATABASE IF NOT EXISTS `service_portal_db`;
USE `service_portal_db`;

CREATE TABLE IF NOT EXISTS `settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `company_name` varchar(255) DEFAULT 'Your Company',
  `company_email` varchar(255) DEFAULT 'contact@example.com',
  `company_whatsapp` varchar(255) DEFAULT '1234567890',
  `upi_id` varchar(255) DEFAULT 'your-upi@ok',
  `terms_conditions` text,
  `footer_text` varchar(255) DEFAULT '© 2025 Your Company. All Rights Reserved.',
  `smtp_host` varchar(255) DEFAULT NULL,
  `smtp_port` int(11) DEFAULT NULL,
  `smtp_user` varchar(255) DEFAULT NULL,
  `smtp_pass` varchar(255) DEFAULT NULL,
  `logo_path` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `settings` (`id`, `company_name`, `company_email`, `company_whatsapp`, `upi_id`, `terms_conditions`, `footer_text`) VALUES
(1, 'Service Portal', 'contact@serviceportal.com', '9876543210', 'upi@okaxis', '1. All services are subject to our terms and conditions.
2. Warranty is applicable only on parts replaced.
3. No warranty on software-related issues.', '© 2025 Service Portal. All Rights Reserved.');

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role` enum('admin','technician') DEFAULT 'admin',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `users` (`id`, `email`, `password`, `role`) VALUES
(1, 'admin@example.com', '$2y$10$g.pA/P1v2RFAzZkC.3r.A.tclHl8tXpr./3v5b2dD2a.i5.Myw0jS', 'admin'); -- password is admin123

CREATE TABLE IF NOT EXISTS `bookings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `booking_id` varchar(10) NOT NULL,
  `customer_name` varchar(255) NOT NULL,
  `customer_mobile` varchar(20) NOT NULL,
  `customer_email` varchar(255) DEFAULT NULL,
  `device_type` varchar(100) NOT NULL,
  `service_type` enum('Normal','Revisit','Warranty') NOT NULL,
  `problem_details` text,
  `photo_path` varchar(255) DEFAULT NULL,
  `status` enum('Booked','Pending','In Progress','Waiting for Parts','On Testing','Completed','Payment','Cancelled') DEFAULT 'Booked',
  `parts_cost` decimal(10,2) DEFAULT '0.00',
  `labour_cost` decimal(10,2) DEFAULT '0.00',
  `total_cost` decimal(10,2) GENERATED ALWAYS AS (`parts_cost` + `labour_cost`) STORED,
  `invoice_pdf_path` varchar(255) DEFAULT NULL,
  `proof_photos_path` text,
  `assigned_to` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `booking_id` (`booking_id`),
  KEY `assigned_to` (`assigned_to`),
  CONSTRAINT `bookings_ibfk_1` FOREIGN KEY (`assigned_to`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `bookings` (`booking_id`, `customer_name`, `customer_mobile`, `customer_email`, `device_type`, `service_type`, `problem_details`, `status`) VALUES
('BK-1001', 'John Doe', '1234567890', 'john.doe@example.com', 'Laptop', 'Normal', 'Screen flickering issue.', 'Booked'),
('BK-1002', 'Jane Smith', '0987654321', 'jane.smith@example.com', 'Smartphone', 'Revisit', 'Battery draining too fast after last service.', 'In Progress');

CREATE TABLE IF NOT EXISTS `device_types` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `device_types` (`name`) VALUES
('Laptop'),
('Smartphone'),
('Tablet'),
('Desktop PC');

CREATE TABLE IF NOT EXISTS `invoices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `booking_id` int(11) NOT NULL,
  `invoice_data` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `booking_id` (`booking_id`),
  CONSTRAINT `invoices_ibfk_1` FOREIGN KEY (`booking_id`) REFERENCES `bookings` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO `invoices` (`booking_id`, `invoice_data`) VALUES
(2, 'Some sample invoice data for booking BK-1002');
