<?php

namespace App\Http\Controllers\Admin;

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

class ArchiveAdminController extends Controller
{
    public function index()
    {
        $archives = Archive::orderBy('id', 'DESC')->paginate();
        return view('admin.archive.index', compact('archives'));
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $archive = new Archive();
        $archive->name = $request->name;
        $filename = $request->slug.'.'. $request->file('file')->getClientOriginalExtension();
        $archive->path = Storage::putFileAs('recursos',$request->file,$filename);
       
        $archive->save();

        return redirect()->route('archive.index');
    }

    /**
     * Display the specified resource.
     */
    public function show(Archive $archive)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Archive $archive)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Archive $archive)
    {
        
        Storage::delete($archive->path);
        $archive->delete();
        
        return redirect()->route('archive.index');
    }
}
