<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Achievement;
use App\Models\ActivityLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class AchievementController extends Controller
{
    public function index()
    {
        $achievements = Achievement::latest()->paginate(20);
        return view('admin.achievements.index', compact('achievements'));
    }

    public function create()
    {
        return view('admin.achievements.create');
    }

    public function show(Achievement $achievement)
    {
        return view('admin.achievements.show', compact('achievement'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'title' => 'required|string|max:255',
            'player_name' => 'required|string|max:255',
            'type' => 'required|in:national,international',
            'medal_type' => 'nullable|in:gold,silver,bronze,none',
            'achievement_date' => 'required|date',
            'description' => 'nullable|string',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:3072',
        ]);

        if ($request->hasFile('photo')) {
            $file = $request->file('photo');
            $name = time() . '_' . Str::slug($data['title']) . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads/achievements'), $name);
            $data['photo'] = 'uploads/achievements/' . $name;
        }

        $achievement = Achievement::create($data);

        ActivityLog::create([
            'user_id' => Auth::id(),
            'action' => 'create_achievement',
            'details' => "Created achievement: {$achievement->title}",
            'ip_address' => request()->ip()
        ]);

        return redirect()->route('admin.achievements.index')->with('success', 'Achievement added.');
    }

    public function edit(Achievement $achievement)
    {
        return view('admin.achievements.edit', compact('achievement'));
    }

    public function update(Request $request, Achievement $achievement)
    {
        $data = $request->validate([
            'title' => 'required|string|max:255',
            'player_name' => 'required|string|max:255',
            'type' => 'required|in:national,international',
            'medal_type' => 'nullable|in:gold,silver,bronze,none',
            'achievement_date' => 'required|date',
            'description' => 'nullable|string',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:3072',
        ]);

        if ($request->hasFile('photo')) {
            if ($achievement->photo && file_exists(public_path($achievement->photo))) {
                @unlink(public_path($achievement->photo));
            }
            $file = $request->file('photo');
            $name = time() . '_' . Str::slug($data['title']) . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('uploads/achievements'), $name);
            $data['photo'] = 'uploads/achievements/' . $name;
        }

        $achievement->update($data);

        ActivityLog::create([
            'user_id' => Auth::id(),
            'action' => 'update_achievement',
            'details' => "Updated achievement: {$achievement->title}",
            'ip_address' => request()->ip()
        ]);

        return redirect()->route('admin.achievements.index')->with('success', 'Achievement updated.');
    }

    public function destroy(Achievement $achievement)
    {
        if ($achievement->photo && file_exists(public_path($achievement->photo))) {
            @unlink(public_path($achievement->photo));
        }
        $achievement->delete();
        return redirect()->route('admin.achievements.index')->with('success', 'Achievement deleted.');
    }
}
