-- QitaKurir V1 - MySQL 8+
-- Import this file into a NEW, EMPTY database created in cPanel.
SET NAMES utf8mb4;
SET time_zone = '+07:00';
SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS businesses (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  business_code VARCHAR(30) NOT NULL UNIQUE,
  business_name VARCHAR(120) NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS branches (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  business_id BIGINT UNSIGNED NOT NULL,
  branch_code VARCHAR(30) NOT NULL,
  branch_name VARCHAR(120) NOT NULL,
  address TEXT NULL,
  phone VARCHAR(30) NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_branch_code (business_id, branch_code),
  CONSTRAINT fk_branches_business FOREIGN KEY (business_id) REFERENCES businesses(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS users (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  branch_id BIGINT UNSIGNED NULL,
  full_name VARCHAR(120) NOT NULL,
  username VARCHAR(60) NOT NULL UNIQUE,
  phone VARCHAR(30) NULL,
  password_hash VARCHAR(255) NOT NULL,
  role ENUM('super_admin','business_admin','branch_admin','employee','courier') NOT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  last_login_at DATETIME NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_users_branch FOREIGN KEY (branch_id) REFERENCES branches(id) ON DELETE SET NULL,
  KEY idx_users_role_active (role, is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS courier_profiles (
  user_id BIGINT UNSIGNED PRIMARY KEY,
  vehicle_type ENUM('motor','mobil','sepeda','jalan_kaki','lainnya') NOT NULL DEFAULT 'motor',
  vehicle_number VARCHAR(30) NULL,
  emergency_contact VARCHAR(30) NULL,
  incentive_type ENUM('per_delivery','daily','monthly','custom') NOT NULL DEFAULT 'per_delivery',
  incentive_amount DECIMAL(12,2) NOT NULL DEFAULT 0,
  CONSTRAINT fk_courier_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS delivery_orders (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_number VARCHAR(40) NOT NULL UNIQUE,
  business_id BIGINT UNSIGNED NOT NULL,
  branch_id BIGINT UNSIGNED NOT NULL,
  created_by BIGINT UNSIGNED NOT NULL,
  customer_name VARCHAR(120) NOT NULL,
  customer_phone VARCHAR(30) NULL,
  pickup_address TEXT NULL,
  delivery_address TEXT NOT NULL,
  delivery_note TEXT NULL,
  item_description TEXT NOT NULL,
  quantity DECIMAL(10,2) NOT NULL DEFAULT 1,
  delivery_fee DECIMAL(12,2) NOT NULL DEFAULT 0,
  fee_payment_status ENUM('unpaid','paid','waived') NOT NULL DEFAULT 'unpaid',
  payment_method ENUM('cash','transfer','qris','other') NULL,
  status ENUM('new','assigned','accepted','picked_up','on_delivery','delivered','failed','cancelled') NOT NULL DEFAULT 'new',
  requested_at DATETIME NULL,
  delivered_at DATETIME NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT fk_orders_business FOREIGN KEY (business_id) REFERENCES businesses(id),
  CONSTRAINT fk_orders_branch FOREIGN KEY (branch_id) REFERENCES branches(id),
  CONSTRAINT fk_orders_creator FOREIGN KEY (created_by) REFERENCES users(id),
  KEY idx_orders_branch_date (branch_id, created_at),
  KEY idx_orders_status_date (status, created_at),
  KEY idx_orders_business_date (business_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS delivery_assignments (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_id BIGINT UNSIGNED NOT NULL,
  courier_id BIGINT UNSIGNED NOT NULL,
  assigned_by BIGINT UNSIGNED NOT NULL,
  assigned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  accepted_at DATETIME NULL,
  finished_at DATETIME NULL,
  assignment_status ENUM('assigned','accepted','rejected','completed','cancelled') NOT NULL DEFAULT 'assigned',
  CONSTRAINT fk_assignment_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE CASCADE,
  CONSTRAINT fk_assignment_courier FOREIGN KEY (courier_id) REFERENCES users(id),
  CONSTRAINT fk_assignment_admin FOREIGN KEY (assigned_by) REFERENCES users(id),
  KEY idx_assignment_courier_status (courier_id, assignment_status),
  KEY idx_assignment_order (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS delivery_status_history (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_id BIGINT UNSIGNED NOT NULL,
  changed_by BIGINT UNSIGNED NOT NULL,
  old_status VARCHAR(30) NULL,
  new_status VARCHAR(30) NOT NULL,
  note TEXT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_status_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE CASCADE,
  CONSTRAINT fk_status_user FOREIGN KEY (changed_by) REFERENCES users(id),
  KEY idx_status_order_date (order_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS delivery_proofs (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_id BIGINT UNSIGNED NOT NULL,
  uploaded_by BIGINT UNSIGNED NOT NULL,
  proof_type ENUM('pickup','delivery','failure') NOT NULL DEFAULT 'delivery',
  file_path VARCHAR(255) NOT NULL,
  recipient_name VARCHAR(120) NULL,
  recipient_note TEXT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_proof_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE CASCADE,
  CONSTRAINT fk_proof_user FOREIGN KEY (uploaded_by) REFERENCES users(id),
  KEY idx_proof_order (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS courier_locations (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  courier_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NULL,
  latitude DECIMAL(10,7) NOT NULL,
  longitude DECIMAL(10,7) NOT NULL,
  recorded_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_location_courier FOREIGN KEY (courier_id) REFERENCES users(id) ON DELETE CASCADE,
  CONSTRAINT fk_location_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE SET NULL,
  KEY idx_location_courier_time (courier_id, recorded_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS delivery_fee_transactions (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  order_id BIGINT UNSIGNED NOT NULL,
  recorded_by BIGINT UNSIGNED NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  transaction_type ENUM('charge','payment','refund','waiver') NOT NULL,
  payment_method ENUM('cash','transfer','qris','other') NULL,
  note VARCHAR(255) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_fee_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE CASCADE,
  CONSTRAINT fk_fee_user FOREIGN KEY (recorded_by) REFERENCES users(id),
  KEY idx_fee_order_date (order_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS courier_incentives (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  courier_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NULL,
  branch_id BIGINT UNSIGNED NOT NULL,
  amount DECIMAL(12,2) NOT NULL DEFAULT 0,
  incentive_date DATE NOT NULL,
  status ENUM('pending','approved','paid','cancelled') NOT NULL DEFAULT 'pending',
  note VARCHAR(255) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_incentive_courier FOREIGN KEY (courier_id) REFERENCES users(id),
  CONSTRAINT fk_incentive_order FOREIGN KEY (order_id) REFERENCES delivery_orders(id) ON DELETE SET NULL,
  CONSTRAINT fk_incentive_branch FOREIGN KEY (branch_id) REFERENCES branches(id),
  KEY idx_incentive_courier_date (courier_id, incentive_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET FOREIGN_KEY_CHECKS = 1;

-- Seed usaha awal; cabang ditambahkan dari menu admin pada tahap berikutnya.
INSERT IGNORE INTO businesses (business_code, business_name) VALUES
('LAUNDRYQU','LaundryQu'),
('QITAMART','Qitamart'),
('QITAKONTER','QitaKonter');


-- Akun awal Super Admin sesuai permintaan pengguna.
-- Username: admin | Password awal: admin
-- Segera ganti password setelah login pertama.
INSERT INTO users (branch_id, full_name, username, phone, password_hash, role, is_active)
SELECT NULL, 'Administrator Utama', 'admin', NULL, '$2y$12$bAnKLY6yazo4X2hJ.8r7Wuu0lHl8jT54rFPDSZYSOZECMSGJi3KHy', 'super_admin', 1
WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'admin');
