<?php
header('Content-Type: application/json; charset=utf-8');
require_once "../config/config.php";
require_once __DIR__ . '/../lib/InvoiceNo.php';

global $pdo;

try {
  if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    throw new RuntimeException('Method not allowed');
  }

  $terminId = (int)($_POST['termin_id'] ?? 0);
  if ($terminId <= 0) {
    throw new RuntimeException('termin_id fehlt');
  }

  // Termin laden
  $st = $pdo->prepare("SELECT * FROM termine WHERE id=:id");
  $st->execute([':id' => $terminId]);
  $t = $st->fetch(PDO::FETCH_ASSOC);

  if (!$t) {
    throw new RuntimeException('Termin nicht gefunden');
  }

  // Prüfen ob Rechnung existiert
  $st = $pdo->prepare("SELECT id FROM invoices WHERE termine_id=:tid LIMIT 1");
  $st->execute([':tid' => $terminId]);

  if ($st->fetch()) {
    throw new RuntimeException('Für diesen Termin existiert bereits eine Rechnung.');
  }

  $serviceDate = date('Y-m-d', (int)$t['startzeit']);
  $invoiceDate = date('Y-m-d');
  $dueDate     = date('Y-m-d', strtotime($invoiceDate . ' +14 days'));

  $year = (int)date('Y');
  $invoiceNo = nextInvoiceNo($pdo, $year);

  $customerName = trim($_POST['customer_name'] ?? 'Kunde (bitte bearbeiten)');
  $customerStreet = trim($_POST['customer_street'] ?? '');
  $customerZip = trim($_POST['customer_zip'] ?? '');
  $customerCity = trim($_POST['customer_city'] ?? '');
  $customerCountry = trim($_POST['customer_country'] ?? 'DE');

  $gage = (int)($t['gage'] ?? 0);

  $desc = "DJ-Leistung am " . date('d.m.Y', (int)$t['startzeit']);

  $pdo->beginTransaction();

  $pdo->prepare("
    INSERT INTO invoices
    (invoice_no, invoice_date, service_date, due_date, status,
     customer_name, customer_street, customer_zip, customer_city, customer_country,
     termine_id, amount_net_eur)
    VALUES
    (:no, :invdate, :servdate, :duedate, 'draft',
     :cname, :cstreet, :czip, :ccity, :ccountry,
     :tid, :amount)
  ")->execute([
    ':no' => $invoiceNo,
    ':invdate' => $invoiceDate,
    ':servdate' => $serviceDate,
    ':duedate' => $dueDate,
    ':cname' => $customerName,
    ':cstreet' => $customerStreet,
    ':czip' => $customerZip,
    ':ccity' => $customerCity,
    ':ccountry' => $customerCountry,
    ':tid' => $terminId,
    ':amount' => $gage
  ]);

  $invoiceId = (int)$pdo->lastInsertId();

  $pdo->prepare("
    INSERT INTO invoice_items
    (invoice_id, pos, description, qty, unit_price_eur, line_total_eur)
    VALUES
    (:iid, 1, :d, 1, :p, :p)
  ")->execute([
    ':iid' => $invoiceId,
    ':d' => $desc,
    ':p' => $gage
  ]);

  $pdo->commit();

  echo json_encode([
    'ok' => true,
    'invoice_id' => $invoiceId,
    'invoice_no' => $invoiceNo
  ], JSON_UNESCAPED_UNICODE);

} catch (Throwable $e) {
  if ($pdo && $pdo->inTransaction()) $pdo->rollBack();

  http_response_code(400);
  echo json_encode([
    'ok' => false,
    'error' => $e->getMessage()
  ], JSON_UNESCAPED_UNICODE);
}