<?php

namespace App\Services\Auction;

use App\Models\AuctionVehicle;
use Illuminate\Support\Str;

class MockJapanAuctionProvider implements AuctionProviderInterface
{
    /**
     * Fetch live auctions from local database or simulated remote payload
     */
    public function getLiveAuctions(array $filters = []): array
    {
        $query = AuctionVehicle::with('auctionHouse');

        if (!empty($filters['make'])) {
            $query->where('make', $filters['make']);
        }
        if (!empty($filters['model'])) {
            $query->where('model', 'like', '%' . $filters['model'] . '%');
        }
        if (!empty($filters['year_min'])) {
            $query->where('year', '>=', (int) $filters['year_min']);
        }
        if (!empty($filters['year_max'])) {
            $query->where('year', '<=', (int) $filters['year_max']);
        }
        if (!empty($filters['auction_house_id'])) {
            $query->where('auction_house_id', $filters['auction_house_id']);
        }
        if (!empty($filters['grade'])) {
            $query->where('auction_grade', $filters['grade']);
        }

        return $query->active()->get()->toArray();
    }

    public function getAuctionVehicle(string $auctionId): ?array
    {
        $vehicle = AuctionVehicle::with('auctionHouse')
            ->where('id', $auctionId)
            ->orWhere('lot_number', $auctionId)
            ->first();

        return $vehicle ? $vehicle->toArray() : null;
    }

    public function getAuctionSheet(string $auctionId): ?string
    {
        $vehicle = AuctionVehicle::where('id', $auctionId)->orWhere('lot_number', $auctionId)->first();
        return $vehicle?->auction_sheet_url;
    }

    public function getAuctionHistory(string $chassisNumber): array
    {
        return [
            [
                'date' => '2024-04-12',
                'auction_house' => 'USS Tokyo',
                'grade' => '4.5',
                'mileage' => '32,000 km',
                'hammer_price_jpy' => 2480000,
                'result' => 'Sold',
            ],
            [
                'date' => '2024-03-05',
                'auction_house' => 'TAA Yokohama',
                'grade' => '4.0',
                'mileage' => '41,500 km',
                'hammer_price_jpy' => 2310000,
                'result' => 'Sold',
            ],
            [
                'date' => '2024-01-18',
                'auction_house' => 'CAA Chubu',
                'grade' => '4.5',
                'mileage' => '29,000 km',
                'hammer_price_jpy' => 2520000,
                'result' => 'Sold',
            ],
        ];
    }

    public function placeBid(string $auctionId, float $maxBidJpy, array $metadata = []): array
    {
        // Simulated API response from Japanese Auction gateway (e.g. USS / ASNET / Aucnet API)
        $txId = 'JP-BID-' . strtoupper(Str::random(10));
        
        return [
            'success' => true,
            'transaction_id' => $txId,
            'auction_lot' => $auctionId,
            'max_authorized_jpy' => $maxBidJpy,
            'status' => 'ACCEPTED_ON_AUCTION_FLOOR',
            'registered_at' => now()->toISOString(),
            'message' => 'Bid successfully queued for live auction floor bidding in Japan.',
            'provider' => 'MockJapanAuctionProvider',
        ];
    }

    public function cancelBid(string $bidExternalId): bool
    {
        return true;
    }

    public function getBidStatus(string $bidExternalId): array
    {
        return [
            'transaction_id' => $bidExternalId,
            'status' => 'ACTIVE',
            'highest_floor_bid_jpy' => 2150000,
            'last_sync_at' => now()->toDateTimeString(),
        ];
    }

    public function getAuctionResult(string $auctionId): array
    {
        $vehicle = AuctionVehicle::where('id', $auctionId)->orWhere('lot_number', $auctionId)->first();
        $hammerPrice = $vehicle ? ($vehicle->starting_bid_jpy + 350000) : 2500000;

        return [
            'auction_lot' => $auctionId,
            'status' => 'WON',
            'hammer_price_jpy' => $hammerPrice,
            'closed_at' => now()->toDateTimeString(),
            'notes' => 'Vehicle successfully secured at or below customer authorized threshold.',
        ];
    }
}
