/home/devscfvi/DevsQuantum/app/Http/Controllers/TeamsController.php
<?php

namespace App\Http\Controllers;

use App\Models\Team;

use Illuminate\Http\Request;

class TeamsController extends Controller
{
 

 public function index()
    {
        $teams = Team::orderBy('sort_order')->get();
        return view('teams.index', compact('teams'));
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('teams.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {



        $request->validate([
            'title' => 'required|string',
            'name' => 'required|string',
            'description' => 'nullable|string',
            'thumbnail' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:20000',


        ]);

        // Get only needed data
        $data = $request->except(['_token', 'thumbnail']);

        if ($request->hasFile('thumbnail')) {
            $thumbnail = $request->file('thumbnail');

            if (!$thumbnail->isValid()) {
                dd('Upload failed. PHP Error Code: ' . $thumbnail->getError()); // <-- important
            }

            $data['thumbnail'] = $thumbnail->store('thumbnails', 'public');
        }


        Team::create($data);

        return redirect()->route('teams.index')->with('success', 'Team created successfully.');
    }



    /**
     * Display the specified resource.
     */
    public function show(Team $team)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Team $team)
    {
        return view('teams.edit', compact('team'));
    }

    public function update(Request $request, team $team)
    {
        $request->validate([
            'name' => 'required|string',
            'title' => 'required|string',
            'description' => 'nullable|string',
            'thumbnail' => 'nullable|image',

        ]);

        $data = $request->except(['_token', '_method', 'thumbnail']);

        if ($request->hasFile('thumbnail')) {
            $data['thumbnail'] = $request->file('thumbnail')->store('thumbnails', 'public');
        }




        $team->update($data);

        return redirect()->route('teams.index')->with('success', 'Team updated successfully.');
    }


    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $team = Team::findOrFail($id);

        // Optional: delete associated images if stored
        if ($team->main_image && Storage::exists($team->main_image)) {
            Storage::delete($team->main_image);
        }

        $team->delete();

        return redirect()->route('teams.index')->with('success', 'Team deleted successfully.');
    }

    public function sort(Request $request)
    {
        foreach ($request->order as $index => $id) {
            Team::where('id', $id)->update(['sort_order' => $index + 1]);
        }

        return response()->json(['status' => 'success']);
    }

}