<?php

declare(strict_types=1);

namespace App\Repositories;

use App\Contracts\Repositories\TransactionRepositoryInterface;
use App\Models\Transaction;
use Illuminate\Database\Eloquent\Collection;

final class EloquentTransactionRepository implements TransactionRepositoryInterface
{
    public function create(array $data): Transaction
    {
        return Transaction::create($data);
    }

    public function update(int $id, array $data): bool
    {
        return Transaction::where('id', $id)->update($data) > 0;
    }

    public function find(int $id): ?Transaction
    {
        return Transaction::find($id);
    }

    public function getByBooking(int $bookingId): ?Transaction
    {
        return Transaction::where('booking_id', $bookingId)->first();
    }

    public function getPendingVerifications(int $ownerId): Collection
    {
        return Transaction::where('owner_id', $ownerId)
            ->where('status', 'payment_verification')
            ->with(['user', 'property', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getNeedsVerification(): Collection
    {
        return Transaction::where('status', 'payment_verification')
            ->with(['user', 'property', 'owner', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getForGuest(int $userId): Collection
    {
        return Transaction::where('user_id', $userId)
            ->with(['property', 'owner', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getForOwner(int $ownerId): Collection
    {
        return Transaction::where('owner_id', $ownerId)
            ->with(['user', 'property', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getForProperty(int $propertyId): Collection
    {
        return Transaction::where('property_id', $propertyId)
            ->with(['user', 'owner', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getRejected(int $userId): Collection
    {
        return Transaction::where('user_id', $userId)
            ->where('status', 'rejected')
            ->with(['property', 'owner', 'booking'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getVerified(int $ownerId): Collection
    {
        return Transaction::where('owner_id', $ownerId)
            ->where('status', 'verified')
            ->with(['user', 'property', 'booking'])
            ->orderBy('verified_at', 'desc')
            ->get();
    }

    public function getCompleted(int $ownerId): Collection
    {
        return Transaction::where('owner_id', $ownerId)
            ->where('status', 'completed')
            ->with(['user', 'property', 'booking'])
            ->orderBy('updated_at', 'desc')
            ->get();
    }

    public function getEarnings(int $ownerId): Collection
    {
        return Transaction::where('owner_id', $ownerId)
            ->whereIn('status', ['verified', 'completed'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function getDisputed(): Collection
    {
        return Transaction::where('status', 'disputed')
            ->with(['user', 'owner', 'property', 'booking', 'disputedBy'])
            ->orderBy('disputed_at', 'desc')
            ->get();
    }
}
