<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\SellerUpgrade;
use App\Models\SellerWithdrawal;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Http\Request;

class MarketplaceController extends Controller
{
    // ── Upgrade requests ────────────────────────────────────────────────────

    public function upgrades(Request $request)
    {
        $query = SellerUpgrade::with('user')->latest();
        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }
        $upgrades = $query->paginate(20);

        return view('backend.marketplace.upgrades', [
            'title'    => 'Seller Upgrade Requests',
            'upgrades' => $upgrades,
        ]);
    }

    public function approveUpgrade($id)
    {
        $upgrade = SellerUpgrade::findOrFail($id);
        $upgrade->update(['status' => 'approved']);
        User::where('id', $upgrade->user_id)->update(['is_seller' => true]);

        return back()->with('success', 'Upgrade approved. User is now a seller.');
    }

    public function rejectUpgrade(Request $request, $id)
    {
        $upgrade = SellerUpgrade::findOrFail($id);
        $upgrade->update(['status' => 'rejected', 'note' => $request->note]);

        return back()->with('success', 'Upgrade request rejected.');
    }

    // ── Withdrawal requests ─────────────────────────────────────────────────

    public function withdrawals(Request $request)
    {
        $query = SellerWithdrawal::with('user')->latest();
        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }
        $withdrawals = $query->paginate(20);

        return view('backend.marketplace.withdrawals', [
            'title'       => 'Seller Withdrawals',
            'withdrawals' => $withdrawals,
        ]);
    }

    public function approveWithdrawal($id)
    {
        $withdrawal = SellerWithdrawal::where('status', 'pending')->findOrFail($id);
        $withdrawal->update(['status' => 'approved']);

        return back()->with('success', 'Withdrawal approved.');
    }

    public function rejectWithdrawal(Request $request, $id)
    {
        $withdrawal = SellerWithdrawal::where('status', 'pending')->findOrFail($id);
        // Refund seller balance
        $withdrawal->user->increment('seller_balance', $withdrawal->amount);
        $withdrawal->update(['status' => 'rejected', 'note' => $request->note]);

        return back()->with('success', 'Withdrawal rejected and balance refunded.');
    }

    // ── Settings ────────────────────────────────────────────────────────────

    public function settings()
    {
        $settings = Setting::first();
        return view('backend.marketplace.settings', [
            'title'    => 'Marketplace Settings',
            'settings' => $settings,
        ]);
    }

    public function updateSettings(Request $request)
    {
        $request->validate([
            'seller_upgrade_fee'    => 'required|numeric|min:0',
            'seller_fee'            => 'required|numeric|min:0|max:100',
            'seller_withdrawal_fee' => 'required|numeric|min:0|max:100',
            'seller_deposit_info'   => 'nullable|string',
        ]);

        $settings = Setting::first();
        $settings->update([
            'seller_upgrade_fee'    => $request->seller_upgrade_fee,
            'seller_fee'            => $request->seller_fee,
            'seller_withdrawal_fee' => $request->seller_withdrawal_fee,
            'seller_deposit_info'   => $request->seller_deposit_info,
        ]);

        return back()->with('success', 'Marketplace settings updated.');
    }
}
