<?php

namespace App\Models;

class ApplicationReceiptModel
{
    protected $db;
    protected $table = "application_receipts";

    public function __construct($db) { $this->db = $db; }

    public function insert(array $data): int
    {
        $sql = "INSERT INTO {$this->table} (application_id, invoice_id, amount, payment_reference, created_at)
                VALUES (:application_id, :invoice_id, :amount, :payment_reference, :created_at)";
        
        $stmt = $this->db->prepare($sql);
        $stmt->execute($data);

        return $this->db->lastInsertId();
    }

    public function findById(int $id): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE id = ?");
        $stmt->execute([$id]);

        return $stmt->fetch() ?: null;
    }

    public function findByInvoice(int $invoiceId): ?array
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE invoice_id = ?");
        $stmt->execute([$invoiceId]);

        return $stmt->fetch() ?: null;
    }

    public function findByStudent(int $studentId): array
    {
        $stmt = $this->db->prepare("SELECT * FROM {$this->table} WHERE student_id = ?");
        $stmt->execute([$studentId]);

        return $stmt->fetchAll();
    }
}
