<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Partner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PartnerController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->input('search');

        $partners = Partner::when($search, function ($query, $search) {
            return $query->where('name', 'LIKE', '%' . $search . '%');
        })->latest()->get();

        return view('admin.partners.index', compact('partners'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255|unique:partners,name',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $logoUrl = null;
        if ($request->hasFile('logo')) {
            $path = $request->file('logo')->store('partners', 'public');
            $logoUrl = Storage::url($path);
        }

        Partner::create([
            'name' => $request->name,
            'logo_url' => $logoUrl,
        ]);

        return redirect()->route('admin.partners.index')->with('success', 'Partner berhasil ditambahkan!');
    }

    public function update(Request $request, Partner $partner)
    {
        $request->validate([
            'name' => 'required|string|max:255|unique:partners,name,' . $partner->id,
            'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $logoUrl = $partner->logo_url;
        if ($request->hasFile('logo')) {
            if ($partner->logo_url) {
                $oldPath = str_replace('/storage/', '', $partner->logo_url);
                Storage::disk('public')->delete($oldPath);
            }
            $path = $request->file('logo')->store('partners', 'public');
            $logoUrl = Storage::url($path);
        }

        $partner->update([
            'name' => $request->name,
            'logo_url' => $logoUrl,
        ]);

        return redirect()->route('admin.partners.index')->with('success', 'Partner berhasil diperbarui!');
    }

    public function destroy(Partner $partner)
    {
        if ($partner->logo_url) {
            $oldPath = str_replace('/storage/', '', $partner->logo_url);
            Storage::disk('public')->delete($oldPath);
        }

        $partner->delete();
        return redirect()->route('admin.partners.index')->with('success', 'Partner berhasil dihapus!');
    }

    // --- FITUR VERIFIKASI SUPERADMIN ---
    public function approve(Partner $partner)
    {
        $partner->update(['status' => 'active']);
        return redirect()->route('admin.partners.index')->with('success', "Partner {$partner->name} berhasil diaktifkan!");
    }

    public function suspend(Partner $partner)
    {
        $partner->update(['status' => 'suspended']);
        return redirect()->route('admin.partners.index')->with('success', "Partner {$partner->name} berhasil dinonaktifkan!");
    }
}

