<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Admin\Repositories\CompanyRepository;
use Modules\Admin\Services\AdminService;

class CompanyController extends Controller
{
    private $companyRepository;
    private $adminService;

    public function __construct(CompanyRepository $companyRepository, AdminService $adminService)
    {
        $this->companyRepository = $companyRepository;
        $this->adminService = $adminService;
    }
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $data['title'] = 'Company Lists';
        $data['companyLists'] = $this->companyRepository->findAll();
        return view('admin::companies.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $data['title'] = 'Create Company';
        $data['editable'] = false;
        $data['companyTypeLists'] = $this->adminService->pluckCompanyTypes();
        return view('admin::companies.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): RedirectResponse
    {
        try {
            $this->companyRepository->create($request->all());
            toastr()->success('Company Created Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('company.index');
    }

    /**
     * Show the specified resource.
     */
    public function show($id)
    {
        return view('admin::companies.show');
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit($id)
    {
        try {
            $data['title'] = 'Edit Company';
            $data['editable'] = true;
            $data['companyTypeLists'] = $this->adminService->pluckCompanyTypes();
            $data['company'] = $this->companyRepository->getCompanyById($id);

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }

        return view('admin::companies.edit', $data);

    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id): RedirectResponse
    {
        try {

            $this->companyRepository->update($id, $request->all());
            toastr()->success('Company Updated Successfully');

        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('company.index');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        try {
            $this->companyRepository->delete($id);
            toastr()->success('Company Deleted Successfully');
        } catch (\Throwable $th) {
            toastr()->error($th->getMessage());
        }
        return redirect()->route('company.index');
    }
}