<?php

namespace App\Http\Controllers\Projets;

use App\Http\Controllers\Controller;
use App\Http\Requests\Projets\Fichiers\UpdateFichier;
use App\Http\Requests\Projets\StoreUpdateTacheGestionProjet;
use App\Models\EtapeProjet;
use App\Models\Fichier;
use App\Models\FichierHasProjet;
use App\Models\Projet;
use App\Models\TacheGestionProjet;
use ETAPE_PROJET;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class TacheController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:edit_projet')->except(['index', 'show']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param int $projet_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function create(int $projet_id)
    {
        if (Projet::checkIfUserCanEditOrDestroy(Projet::find($projet_id), true)) {
            // On ne retourne pas une vue ici car on est à l'intérieur d'un projet
            return redirect()->route('projets.edit', $projet_id)->with(['active' => 'gestion_projet.form', 'route_tache' => 'store']);
        } else {
            abort(403);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $projet_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(StoreUpdateTacheGestionProjet $request, int $projet_id)
    {
        $projet = Projet::find($projet_id);
        if (isset($projet)) {
            if (Projet::checkIfUserCanEditOrDestroy(Projet::find($projet_id), true)) {
                $request->validated();

                $date_debut = date_create_from_format('d/m/Y', $request->date_debut);
                $date_fin = date_create_from_format('d/m/Y', $request->date_fin);
                TacheGestionProjet::firstOrCreate([
                    'projet_id' => $projet_id,
                    'nom' => $request->nom,
                    'description' => $request->description,
                    'responsable_id' => $request->responsable_id,
                    'date_debut' => $date_debut,
                    'date_fin' => $date_fin,
                    'nb_jours' => $request->nb_jours
                ]);

                // On ajoute l'étape 'gestion_projet'
                EtapeProjet::firstOrCreate([
                    'etape' => ETAPE_PROJET::GESTION_PROJET,
                    'projet_id' => $projet_id
                ]);

                return redirect()->route('projets.edit', $projet->id)->with([
                    'success' => 'Tâche enregistrée avec succès.',
                    'active' => 'gestion_projet'
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $projet_id
     * @param int $tache_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function edit(int $projet_id, int $tache_id)
    {
        $projet = Projet::find($projet_id);
        $tache = TacheGestionProjet::find($tache_id);

        if (isset($projet) && isset($tache)) {
            if (Projet::checkIfUserCanEditOrDestroy(Projet::find($projet_id), true)) {
                // On ne retourne pas une vue ici car on est à l'intérieur d'un projet
                return redirect()->route('projets.edit', [$projet->id, $tache])
                ->with([
                    'tache' => $tache, // On passe le fichier en session que l'on récupère dans le projet controller -> getListOnglet()
                    'active' => 'gestion_projet.form',
                    'route_tache' => 'update'
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param StoreUpdateTacheGestionProjet $request
     * @param int $projet_id
     * @param int $tache_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(StoreUpdateTacheGestionProjet $request, int $projet_id, int $tache_id)
    {
        $projet = Projet::find($projet_id);
        $tache = TacheGestionProjet::find($tache_id);

        if (isset($projet) && isset($tache)) {
            if (Projet::checkIfUserCanEditOrDestroy(Projet::find($projet_id), true)) {
                $request->validated();

                $date_debut = date_create_from_format('d/m/Y', $request->date_debut);
                $date_fin = date_create_from_format('d/m/Y', $request->date_fin);
                $tache->update([
                    'nom' => $request->nom,
                    'description' => $request->description,
                    'responsable_id' => $request->responsable_id,
                    'date_debut' => $date_debut,
                    'date_fin' => $date_fin,
                    'nb_jours' => $request->nb_jours
                ]);
                return redirect()->route('projets.edit', $projet->id)->with([
                    'success' => 'Tâche mis à jour avec succès.',
                    'active' => 'gestion_projet'
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $projet_id
     * @param int $fichier_id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(int $projet_id, int $tache_id)
    {
        $projet = Projet::find($projet_id);
        $tache = TacheGestionProjet::find($tache_id);
        if (isset($projet) && isset($tache)) {
            if (Projet::checkIfUserCanEditOrDestroy(Projet::find($projet_id), true)) {
                $tache->delete();
                return redirect()->route('projets.edit', $projet->id)->with([
                    'success' => 'La tâche  a été supprimée avec succès.',
                    'active' => 'gestion_projet'
                ]);
            } else {
                abort(403);
            }
        } else {
            abort(404);
        }
    }
}
