<?php

require_once __DIR__ . '/../config/database.php';

class PricingEngine
{

    private PDO $db;

    public function __construct()
    {
        $database = new Database();
        $this->db = $database->connect();
    }


    /**
     * Calculate Printing Cost
     */
    public function calculate(

        int $pages,
        int $copies,
        string $paperSize,
        string $printType,
        string $duplex

    ): array
    {

        $sql = "

            SELECT *

            FROM pricing

            WHERE

            paper_size = ?

            AND print_type = ?

            AND duplex = ?

            LIMIT 1

        ";

        $stmt = $this->db->prepare($sql);

        $stmt->execute([

            $paperSize,
            $printType,
            $duplex

        ]);

        $pricing = $stmt->fetch(PDO::FETCH_ASSOC);


        if (!$pricing) {

            return [

                "success" => false,

                "message" => "Pricing not found."

            ];

        }


        $pricePerPage = (float)$pricing['price_per_page'];

        $subtotal =

            $pages *

            $copies *

            $pricePerPage;


        return [

            "success" => true,

            "pages" => $pages,

            "copies" => $copies,

            "paper_size" => $paperSize,

            "print_type" => $printType,

            "duplex" => $duplex,

            "price_per_page" => $pricePerPage,

            "subtotal" => $subtotal,

            "total" => $subtotal

        ];

    }

}