<?php

function apply_penalty(PDO $pdo, int $sp_company_id, int $breakdown_id, string $reason, int $points = 1)
{
    /* Log penalty */
    $stmt = $pdo->prepare("
        INSERT INTO sp_penalty_log (sp_company_id, breakdown_id, reason, penalty_points)
        VALUES (?, ?, ?, ?)
    ");
    $stmt->execute([$sp_company_id, $breakdown_id, $reason, $points]);

    /* Increment company total */
    $stmt = $pdo->prepare("
        UPDATE companies
        SET penalty_points = penalty_points + ?
        WHERE id = ?
    ");
    $stmt->execute([$points, $sp_company_id]);
}