<?php

namespace App\Http\Controllers\Dashboard\ContractTemplate;

use App\Http\Controllers\Controller as ExController;
use Illuminate\Http\Request;
use App\Models\ContractTemplate;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class ContractTemplateController extends ExController
{
    public function index(Request $request)
    {
        $templates = ContractTemplate::latest('id')->paginate(30);

        return view('dashboard.contract-templates.index', compact('templates'));
    }

    public function create()
    {
        return view('dashboard.contract-templates.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'lang' => 'required|in:uz,ru',
            'type' => 'required|in:legal,physical',
            'file' => 'required|file|mimes:doc,docx',
            'company' => 'required|in:getgreen,sunhightech',
        ]);

        $folder = "contract-templates";
        $fileName = $request->file('file')->store($folder);

        ContractTemplate::create([
            'lang' => $request->lang,
            'type' => $request->type,
            'company' => $request->company,
            'path' => $fileName,
        ]);

        $this->info(trans('admin.messages.created'));
        return redirect()->route('dashboard.contract-templates.index');
    }

    public function edit($templateId)
    {
        $template = ContractTemplate::findOrFail($templateId);
        return view('dashboard.contract-templates.edit', compact('template'));
    }

    public function update(Request $request, $templateId)
    {
        $template = ContractTemplate::findOrFail($templateId);

        $this->validate($request, [
            'file' => 'nullable|file|mimes:doc,docx',
            'lang' => 'required|in:uz,ru',
            'company' => 'required|in:getgreen,sunhightech',
            'type' => 'required|in:legal,physical',
        ]);

        $template->update([
            'lang' => $request->lang,
            'company' => $request->company,
            'type' => $request->type,
        ]);

        if ($request->hasFile('file')) {
            // delete old file
            if ($template->path) {
                Storage::disk('public')->delete($template->path);
            }

            // store file to storage
            $file = $request->file('file');
            // with generated name
            // generate name with 20 string
            $fileName = Str::random(20) . '.' . $file->extension();
            $file->storeAs('contract-templates', $fileName, 's3');

            $template->update([
                'path' => 'contract-templates/' . $fileName,
            ]);
        }

        $this->info(trans('admin.messages.updated'));
        return redirect()->route('dashboard.contract-templates.index');
    }

    public function destroy($templateId)
    {
        $template = ContractTemplate::findOrFail($templateId);

        if ($template->path) {
            Storage::disk('public')->delete($template->path);
        }

        $template->delete();

        $this->info(trans('admin.messages.deleted'));
        return redirect()->back();
    }
}
