<?php
namespace App\Repositories;

use App\Database\Connection;
use App\Models\DealPayment;
use PDO;

class DealPaymentRepository
{
    private $db;

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

    public function getByDealDidarId(string $dealDidarId): array
    {
        if (trim($dealDidarId) === '') {
            return [];
        }
        $stmt = $this->db->getPdo()->prepare("
            SELECT * FROM deal_payments
            WHERE deal_didar_id = ?
            ORDER BY receipt_date ASC, id ASC
        ");
        $stmt->execute([$dealDidarId]);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
        return array_map(static function ($row) {
            return new DealPayment($row);
        }, $rows);
    }

    public function save(DealPayment $payment): int
    {
        $pdo = $this->db->getPdo();
        if (!empty($payment->id)) {
            $stmt = $pdo->prepare("
                UPDATE deal_payments
                SET payment_type = ?, payment_status = ?, payment_amount = ?, receipt_date = ?, cheque_due_date = ?, cheque_cashed_date = ?,
                    reference_no = ?, notes = ?, created_by_user_id = ?
                WHERE id = ?
            ");
            $stmt->execute([
                strtoupper(trim((string)$payment->payment_type)) ?: 'CASH',
                strtoupper(trim((string)$payment->payment_status)) ?: 'PENDING',
                (float)$payment->payment_amount,
                $payment->receipt_date ?: null,
                $payment->cheque_due_date ?: null,
                $payment->cheque_cashed_date ?: null,
                $payment->reference_no ?: null,
                $payment->notes ?: null,
                $payment->created_by_user_id ? (int)$payment->created_by_user_id : null,
                (int)$payment->id,
            ]);
            return (int)$payment->id;
        }

        $stmt = $pdo->prepare("
            INSERT INTO deal_payments
                (deal_didar_id, payment_type, payment_status, payment_amount, receipt_date, cheque_due_date, cheque_cashed_date, reference_no, notes, created_by_user_id)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([
            $payment->deal_didar_id,
            strtoupper(trim((string)$payment->payment_type)) ?: 'CASH',
            strtoupper(trim((string)$payment->payment_status)) ?: 'PENDING',
            (float)$payment->payment_amount,
            $payment->receipt_date ?: null,
            $payment->cheque_due_date ?: null,
            $payment->cheque_cashed_date ?: null,
            $payment->reference_no ?: null,
            $payment->notes ?: null,
            $payment->created_by_user_id ? (int)$payment->created_by_user_id : null,
        ]);
        return (int)$pdo->lastInsertId();
    }

    public function findById(int $id): ?DealPayment
    {
        if ($id <= 0) {
            return null;
        }
        $stmt = $this->db->getPdo()->prepare("SELECT * FROM deal_payments WHERE id = ? LIMIT 1");
        $stmt->execute([$id]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row ? new DealPayment($row) : null;
    }
}
