<?php
require_once 'db.php';
header('Content-Type: application/json');
$pdo = db();
$method = $_SERVER['REQUEST_METHOD'];
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'list';

function out($data, $code = 200) {
  http_response_code($code);
  echo json_encode($data);
  exit;
}

try {
  if ($method === 'GET' && $action === 'list') {
    $stmt = $pdo->query("SELECT * FROM shippers WHERE IFNULL(is_deleted,0)=0 ORDER BY id DESC");
    out(['ok' => true, 'data' => $stmt->fetchAll()]);
  }

  if ($method === 'GET' && $action === 'get') {
    $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
    $stmt = $pdo->prepare("SELECT * FROM shippers WHERE id = ?");
    $stmt->execute([$id]);
    $row = $stmt->fetch();
    if ($row) out(['ok' => true, 'data' => $row]);
    out(['ok' => false, 'error' => 'Not found'], 404);
  }

  if ($method === 'POST' && $action === 'create') {
    $shipper_name = isset($_POST['shipper_name']) ? trim($_POST['shipper_name']) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $address = isset($_POST['address']) ? trim($_POST['address']) : '';
    $country = isset($_POST['country']) ? trim($_POST['country']) : '';
    $contact_number = isset($_POST['contact_number']) ? trim($_POST['contact_number']) : '';
    $second_phone = isset($_POST['second_phone']) ? trim($_POST['second_phone']) : '';
    $business_name = isset($_POST['business_name']) ? trim($_POST['business_name']) : '';
    if (!$shipper_name) out(['ok' => false, 'error' => 'Name required'], 400);
    if (!$contact_number) out(['ok' => false, 'error' => 'Contact required'], 400);
    $stmt = $pdo->prepare("
      INSERT INTO shippers (shipper_name, email, address, country, contact_number, second_phone, business_name)
      VALUES (?, ?, ?, ?, ?, ?, ?)
    ");
    $stmt->execute([$shipper_name, $email, $address, $country, $contact_number, $second_phone, $business_name]);
    out(['ok' => true, 'id' => (int)$pdo->lastInsertId(), 'shipper_name' => $shipper_name]);
  }

  if ($method === 'POST' && $action === 'update') {
    $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    if (!$id) out(['ok' => false, 'error' => 'id required'], 400);
    $shipper_name = isset($_POST['shipper_name']) ? trim($_POST['shipper_name']) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';
    $address = isset($_POST['address']) ? trim($_POST['address']) : '';
    $country = isset($_POST['country']) ? trim($_POST['country']) : '';
    $contact_number = isset($_POST['contact_number']) ? trim($_POST['contact_number']) : '';
    $second_phone = isset($_POST['second_phone']) ? trim($_POST['second_phone']) : '';
    $business_name = isset($_POST['business_name']) ? trim($_POST['business_name']) : '';
    if (!$shipper_name) out(['ok' => false, 'error' => 'Name required'], 400);
    if (!$contact_number) out(['ok' => false, 'error' => 'Contact required'], 400);
    $stmt = $pdo->prepare("
      UPDATE shippers SET shipper_name=?, email=?, address=?, country=?, contact_number=?, second_phone=?, business_name=? WHERE id=?
    ");
    $stmt->execute([$shipper_name, $email, $address, $country, $contact_number, $second_phone, $business_name, $id]);
    out(['ok' => true]);
  }

  if ($method === 'POST' && $action === 'delete') {
    $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    if (!$id) out(['ok' => false, 'error' => 'id required'], 400);
    // Soft delete to avoid FK restriction
    $stmt = $pdo->prepare("UPDATE shippers SET is_deleted=1 WHERE id = ?");
    $stmt->execute([$id]);
    out(['ok' => true]);
  }

  out(['ok' => false, 'error' => 'invalid request'], 400);
} catch (Throwable $e) {
  out(['ok' => false, 'error' => $e->getMessage()], 500);
}
