<?php
namespace App\Controllers;

use App\Core\Controller;
use App\Models\Property;
use App\Models\RentPayment;

class RentController extends Controller
{
    public function properties()
    {
        $this->requireAuth();
        $propertyModel = new Property();
        $properties = $propertyModel->getAll();
        $this->render('rent/properties', ['properties' => $properties]);
    }

    public function createPropertyForm()
    {
        $this->requireAuth();
        $this->render('rent/create_property');
    }

    public function storeProperty()
    {
        $this->requireAuth();
        $model = new Property();
        $data = [
            'name' => $_POST['name'],
            'address' => $_POST['address'],
            'monthly_rent' => $_POST['monthly_rent'],
            'tenant_name' => $_POST['tenant_name'],
            'tenant_phone' => $_POST['tenant_phone'],
            'status' => $_POST['status']
        ];
        $model->create($data);
        $this->redirect('/rent/properties');
    }

    public function payments()
    {
        $this->requireAuth();
        $paymentModel = new RentPayment();
        $payments = $paymentModel->getAll();
        $this->render('rent/payments', ['payments' => $payments]);
    }

    public function createPaymentForm()
    {
        $this->requireAuth();
        $propertyModel = new Property();
        $properties = $propertyModel->getAll();
        $this->render('rent/create_payment', ['properties' => $properties]);
    }

    public function storePayment()
    {
        $this->requireAuth();
        $model = new RentPayment();
        $data = [
            'property_id' => $_POST['property_id'],
            'payment_date' => $_POST['payment_date'],
            'amount' => $_POST['amount'],
            'period_month' => $_POST['period_month'],
            'payment_method' => $_POST['payment_method'],
            'reference' => $_POST['reference'] ?? '',
            'notes' => $_POST['notes'] ?? '',
            'recorded_by' => $_SESSION['user_id']
        ];
        $model->create($data);
        $this->redirect('/rent/payments');
    }
}