<?php
declare(strict_types=1);

/**
 * Demo commerce + customer portal logins.
 * Expected: $pdo (PDO), $hash (password hash for Welcome1!).
 */
/** @var PDO $pdo */
/** @var string $hash */

$pages = json_encode([
    'invoices' => true,
    'documents' => true,
    'sign' => true,
    'orders' => true,
    'contract' => true,
    'service_request' => true,
    'equipment' => true,
], JSON_UNESCAPED_UNICODE);
$systems = json_encode([
    'orders' => 'connected',
    'accounting' => 'connected',
    'service' => 'connected',
], JSON_UNESCAPED_UNICODE);

$pdo->prepare(
    'INSERT INTO tenant_commerce (tenant_id, customer_portal_on, customer_pages, systems) VALUES (1, 1, ?, ?)
     ON DUPLICATE KEY UPDATE customer_portal_on = 1, customer_pages = VALUES(customer_pages), systems = VALUES(systems)'
)->execute([$pages, $systems]);

$cu = $pdo->prepare(
    'INSERT INTO customer_users (tenant_id, account_id, email, password_hash, first_name, last_name, status)
     VALUES (1,?,?,?,?,?,\'active\')'
);
$customers = [
    [1, 'mike@abcoffice.example', $hash, 'Mike', 'Johnson'],
    [4, 'lisa@techsolutions.example', $hash, 'Lisa', 'Anderson'],
    [36, 'buyer@copiercare.example', $hash, 'Chris', 'Ng'],
];
foreach ($customers as $c) {
    try {
        $cu->execute($c);
    } catch (PDOException $e) {
        if (!str_contains($e->getMessage(), 'Duplicate')) {
            throw $e;
        }
    }
}

$exists = (int) $pdo->query('SELECT COUNT(*) FROM quotes WHERE tenant_id = 1')->fetchColumn();
if ($exists > 0) {
    return;
}

$pdo->exec("INSERT INTO quotes (id, tenant_id, account_id, opportunity_id, owner_id, number, title, amount, status, body, sent_at, approved_at, approved_by) VALUES
(1,1,1,NULL,2,'Q-0001','Furniture upgrade quote',4200.00,'sent','Desk run and seating for ABC Office.','2026-08-12 10:00:00','2026-08-11 09:00:00',1),
(2,1,2,NULL,2,'Q-0002','Warehouse supplies',8900.00,'pending_approval','Bulk MRO for Acme. Needs manager yes.',NULL,NULL,NULL),
(3,1,4,NULL,2,'Q-0003','Annual software seats',12600.00,'approved','12 seats, billed yearly.',NULL,'2026-08-20 14:00:00',1)");

$pdo->exec("INSERT INTO orders (id, tenant_id, account_id, opportunity_id, quote_id, owner_id, number, title, amount, status, ordered_at) VALUES
(1,1,1,NULL,1,2,'SO-0001','Furniture upgrade',4200.00,'fulfilled','2026-08-15'),
(2,1,6,NULL,NULL,2,'SO-0002','Catalog restock',1800.00,'open','2026-08-28'),
(3,1,13,NULL,NULL,2,'SO-0003','Toner and paper',640.00,'fulfilled','2026-06-02')");

$pdo->exec("INSERT INTO contracts (id, tenant_id, account_id, opportunity_id, owner_id, number, title, amount, start_date, end_date, renewal_date, status) VALUES
(1,1,4,NULL,2,'CT-0001','Software subscription',12600.00,DATE_SUB(CURDATE(), INTERVAL 8 MONTH),DATE_ADD(CURDATE(), INTERVAL 4 MONTH),DATE_ADD(CURDATE(), INTERVAL 4 MONTH),'active'),
(2,1,9,NULL,2,'CT-0002','Hotel supply agreement',5400.00,DATE_SUB(CURDATE(), INTERVAL 10 MONTH),DATE_ADD(CURDATE(), INTERVAL 20 DAY),DATE_ADD(CURDATE(), INTERVAL 20 DAY),'active')");

$pdo->exec("INSERT INTO invoices (id, tenant_id, account_id, order_id, owner_id, number, title, amount, paid_amount, status, due_date, paid_at) VALUES
(1,1,1,1,2,'INV-0001','Furniture upgrade',4200.00,4200.00,'paid',DATE_SUB(CURDATE(), INTERVAL 10 DAY),DATE_SUB(NOW(), INTERVAL 8 DAY)),
(2,1,6,2,2,'INV-0002','Catalog restock',1800.00,0.00,'unpaid',DATE_ADD(CURDATE(), INTERVAL 12 DAY),NULL),
(3,1,4,NULL,2,'INV-0003','Software seats',12600.00,4000.00,'partial',DATE_ADD(CURDATE(), INTERVAL 5 DAY),NULL)");

$pdo->exec("INSERT INTO portal_documents (id, tenant_id, account_id, owner_id, related_type, related_id, title, kind, body, shared) VALUES
(1,1,1,2,'quote',1,'Furniture upgrade quote','quote','Quote Q-0001 for ABC Office Supplies. Amount $4,200.',1),
(2,1,4,2,'contract',1,'Software subscription','esign','Please sign to confirm the 12-seat software agreement.',1),
(3,1,36,4,'file',NULL,'Site service terms','file','Standard on-site service terms for CopierCare Downtown.',1)");

$pdo->exec("INSERT INTO signatures (id, tenant_id, document_id, account_id, status) VALUES
(1,1,2,4,'pending')");

$pdo->exec("INSERT INTO service_jobs (id, tenant_id, account_id, opportunity_id, assignee_id, number, subject, body, status, source) VALUES
(1,1,36,NULL,4,'JOB-0001','Paper jam on floor 2','Customer reported repeating jam on the lobby copier.','open','staff'),
(2,1,37,NULL,4,'JOB-0002','Quarterly maintenance','Preventive visit booked from last service.','in_progress','staff'),
(3,1,36,NULL,4,'JOB-0003','Toner swap request','Customer asked for black toner on the Customer Portal.','open','customer')");

$pdo->exec("INSERT INTO job_notes (tenant_id, job_id, user_id, body) VALUES
(1,2,4,'Checked rollers. Need a kit on the next stop.')");

$pdo->exec("INSERT INTO equipment (id, tenant_id, account_id, name, model, serial_no, location, status, installed_at) VALUES
(1,1,36,'Lobby copier','AOS-C500','SN-C500-1102','Floor 2 lobby','active','2024-03-12'),
(2,1,36,'Accounting printer','AOS-P200','SN-P200-441','Accounting','active','2025-01-08'),
(3,1,37,'Production copier','AOS-C900','SN-C900-008','Press room','active','2023-11-01')");
