CREATE TABLE IF NOT EXISTS `credit_customers` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(255) NOT NULL,
  `contact` VARCHAR(255) NULL,
  `address` TEXT NULL,
  `credit_limit` DECIMAL(10, 2) DEFAULT 0.00,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `vehicles` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `customer_id` INT NOT NULL,
  `vehicle_number` VARCHAR(255) NOT NULL,
  `make` VARCHAR(255) NULL,
  `model` VARCHAR(255) NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`customer_id`) REFERENCES `credit_customers`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `credit_sales` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `date` DATE NOT NULL,
  `customer_id` INT NOT NULL,
  `vehicle_id` INT NULL,
  `fuel_type_id` INT NOT NULL,
  `quantity` DECIMAL(10, 2) NOT NULL,
  `rate` DECIMAL(10, 2) NOT NULL,
  `amount` DECIMAL(10, 2) NOT NULL,
  `is_settled` BOOLEAN DEFAULT FALSE,
  `settled_at` DATETIME NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`customer_id`) REFERENCES `credit_customers`(`id`),
  FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles`(`id`),
  FOREIGN KEY (`fuel_type_id`) REFERENCES `fuel_types`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
