<?php
namespace App\Models;

use App\Core\Model;
use PDO;

class RentPayment extends Model
{
    /**
     * Get all rent payments (with optional limit)
     */
    public function getAll(int $limit = 100): array
    {
        $sql = "SELECT rp.*, p.name as property_name 
                FROM rent_payments rp
                JOIN properties p ON rp.property_id = p.id
                ORDER BY rp.payment_date DESC 
                LIMIT :limit";
        
        $stmt = self::getPDO()->prepare($sql);
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function create(array $data): int
    {
        return $this->insert('rent_payments', $data);
    }

    public function getTotalForMonth(string $yearMonth): float
    {
        $stmt = $this->query(
            "SELECT SUM(amount) FROM rent_payments WHERE DATE_FORMAT(payment_date, '%Y-%m') = ?",
            [$yearMonth]
        );
        return (float) ($stmt->fetchColumn() ?: 0);
    }
}