<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\ApiActive;
use App\Models\ApiProvider;
use App\Models\Provider;
use App\Models\Games;
use App\Http\Controllers\Api\SeamlesWsController;


class ProviderController extends Controller
{

    public function index(Request $request)
    {
        $apiss = ApiProvider::all();
        $api = ApiActive::first();
        $ip = '123.456.789';
        return view('backend.api', compact('apiss', 'api','ip'));
    }


    public function call(Request $request)
    {
        $api = new SeamlesWsController();
        $playing = $api->call_players();
        $api = ApiActive::first();

        return view('backend.call', compact('playing'));
    }

    public function call_players(Request $request)
    {
        $api = new SeamlesWsController();
        $playing = $api->call_players();

        return $playing;
    }

    public function call_list(Request $request)
    {
        $api = new SeamlesWsController();
        $playing = $api->call_list('PGSOFT','mahjong-ways');

        return $playing;
    }

    public function call_apply(Request $request)
    {
        $api = new SeamlesWsController();
        $playing = $api->call_apply(
    $request->providerCode,
    $request->gameCode,
    $request->userCode,
    $request->integer('callRtp'),
    $request->integer('callType')
);

        return $playing;
    }

    public function rtp(Request $request)
    {
        $api = new SeamlesWsController();
        $playing = $api->control_rtp($request->providerCode,$request->userCode,$request->rtp);

        return $playing;
    }

    public function edit($id, Request $request)
    {
        $apiss = ApiProvider::find($id);
        $apiss->apikey = $request->apikey;
        $apiss->secretkey = $request->secretkey;
        $apiss->url = $request->endpoint;
        $apiss->save();

        return back()->with('success', 'API Successfully update');
    }

    public function use($id, Request $request)
    {
        if (!$request->accesskey) {
            return response()->json([
                'status' => 'error',
                'message' => 'Invalid Key'
            ]);
        } elseif ($request->accesskey != env('APP_SECKEY')) {
            return response()->json([
                'status' => 'error',
                'message' => 'Invalid Key'
            ]);
        }
        $apiss = ApiProvider::find($id);

        $api = ApiActive::first();
        $api->provider_id = $apiss->id;
        $api->title = $apiss->provider;
        $api->save();

        return response()->json([
            'status' => 'success',
            'message' => 'API Successfully update'
        ]);
    }
    
    public function provider(Request $request)
    {
        $provider = Provider::all();
        return view('backend.provider.index', compact('provider'));
    }
    
    public function provideredit($id, Request $request)
    {
        $provider = Provider::all();
        $providers = Provider::find($id);
        return view('backend.provider.index', compact('provider', 'providers'));
    }
    
    public function providercreate(Request $request)
    {
        // 1. Validasi Input
        $request->validate([
            'provider'       => 'required|string|max:255',
            'slug'           => 'required|string|max:255',
            'icon'           => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
            'banner_desktop' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
            'banner_mobile'  => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
        ]);
    
        $provider = new Provider();
    
        // 2. Siapkan variabel path gambar (default null)
        $iconPath = null;
        $bannerDesktopPath = null;
        $bannerMobilePath = null;
    
        // 3. Cek dan proses upload ke disk 'uploads'
        if ($request->hasFile('icon')) {
            $iconPath = $request->file('icon')->store('providers/icons', 'uploads');
        }
        if ($request->hasFile('banner_desktop')) {
            $bannerDesktopPath = $request->file('banner_desktop')->store('providers/banners_desktop', 'uploads');
        }
        if ($request->hasFile('banner_mobile')) {
            $bannerMobilePath = $request->file('banner_mobile')->store('providers/banners_mobile', 'uploads');
        }
    
        // 4. Masukkan path gambar ke database
        $provider->icon = $iconPath;
        $provider->banner_desktop = $bannerDesktopPath;
        $provider->banner_mobile = $bannerMobilePath;
    
        // Masukkan data lainnya
        $provider->provider_name = e(strip_tags($request->provider));
        $provider->slug          = e(strip_tags($request->slug));
        $provider->provider_code = e($request->provider_code);
        $provider->game_type     = e(strip_tags($request->type));
        $provider->product_code  = e($request->product_code);
        $provider->status        = $request->integer('status');
        $provider->save();
    
        return back()->with('success', 'Provider Successfully Created');
    }

    public function providerupdate($id, Request $request)
    {
        // 1. Validasi Input
        $request->validate([
            'provider'       => 'required|string|max:255',
            'slug'           => 'required|string|max:255',
            'icon'           => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
            'banner_desktop' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
            'banner_mobile'  => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
        ]);
    
        $provider = Provider::findOrFail($id); 
    
        // 2. Proses Update File ke disk 'uploads' & Hapus File Lama
        if ($request->hasFile('icon')) {
            // Hapus yang lama dari disk 'uploads'
            if ($provider->icon && Storage::disk('uploads')->exists($provider->icon)) {
                Storage::disk('uploads')->delete($provider->icon);
            }
            // Simpan yang baru
            $provider->icon = $request->file('icon')->store('providers/icons', 'uploads');
        }
    
        if ($request->hasFile('banner_desktop')) {
            if ($provider->banner_desktop && Storage::disk('uploads')->exists($provider->banner_desktop)) {
                Storage::disk('uploads')->delete($provider->banner_desktop);
            }
            $provider->banner_desktop = $request->file('banner_desktop')->store('providers/banners_desktop', 'uploads');
        }
    
        if ($request->hasFile('banner_mobile')) {
            if ($provider->banner_mobile && Storage::disk('uploads')->exists($provider->banner_mobile)) {
                Storage::disk('uploads')->delete($provider->banner_mobile);
            }
            $provider->banner_mobile = $request->file('banner_mobile')->store('providers/banners_mobile', 'uploads');
        }
    
        // Update data lainnya
        $provider->provider_name = e(strip_tags($request->provider));
        $provider->slug          = e(strip_tags($request->slug));
        $provider->provider_code = e($request->provider_code);
        $provider->game_type     = e(strip_tags($request->type));
        $provider->product_code  = e($request->product_code);
        $provider->status        = $request->integer('status');
        $provider->save();
        
        return back()->with('success', 'Provider Successfully updated');
    }
    
    public function providerdelete($id, Request $request)
    {
        $provider = Provider::findOrFail($id);
        $provider->delete();

        return back()->with('success','Provider Successfully deleted');

    }
    
    public function games(Request $request)
    {
        $provider = Provider::all();
        $games = Games::where('provider', $request->provider)->get();
        return view('backend.provider.games', compact('provider', 'games'));
    }
}
