<?php

namespace App\Http\Controllers\CRM;

use Illuminate\Http\Request;
use App\Models\CRM\Company;
use Illuminate\Contracts\View\View;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CRM\StoreCompanyRequest;
use App\Http\Requests\CRM\UpdateCompanyRequest;

class CompanyController extends Controller
{

    public function index(): View
    {
        $companies = Company::latest()->take(10)->get();
        return view('backend.crm.companies.index', compact('companies'));
    }

    public function create(): View
    {
        $readonly = null;
        return view('backend.crm.companies.dashboard', [
            'company' => null,
            'readonly' => $readonly
        ]);
    }

    public function store(StoreCompanyRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $company = Company::create($data);

        return redirect()->route('crm.companies.edit', $company)
            ->with('status', 'Success');
    }


    public function edit(Company $company): View
    {
        $company->load(['entity']);
        $readonly = null;
        return view('backend.crm.companies.dashboard', compact('company', 'readonly'));
    }

    public function update(UpdateCompanyRequest $request, Company $company): RedirectResponse
    {
        $data = $request->validated();
        $company->update($data);
        return redirect()->route('crm.companies.edit', $company)
            ->with('status', 'Success');
    }


        public function data(Request $request)
    {
        $query = Company::query();

        // global search
        if ($request->filled('search')) {
            $search = $request->input('search');
            $query->where(function ($q) use ($search) {
                $q->where('business_name', 'like', "%{$search}%")
                    ->orWhere('short_name',    'like', "%{$search}%")
                    ->orWhere('mb',            'like', "%{$search}%")
                    ->orWhere('pib',           'like', "%{$search}%")
                    ->orWhere('email',         'like', "%{$search}%");
            });
        }

        // sorting
        $sort      = $request->query('sort', 'id');
        $direction = $request->query('direction', 'asc');
        $query->orderBy($sort, $direction);

        // pagination (10 per page)
        $companies = $query->paginate(10);

        return response()->json([
            'data'         => $companies->items(),
            'current_page' => $companies->currentPage(),
            'last_page'    => $companies->lastPage(),
            'total'        => $companies->total(),
        ]);
    }

    // public function destroy(Company $company): RedirectResponse
    // {
    //     $company->delete();
    //     return redirect()->route('crm.companies.index')
    //         ->with('status', 'Success');
    // }



    // public function show(Company $company): View
    // {
    //     return view('backend.crm.companies.show', compact('company'));
    // }









    // public function create(Company $company = null): View
    // {
    //     $readonly = null;
    //     return view('backend.crm.companies.dashboard', compact('company', 'readonly'));
    // }

    // public function store(StoreCompanyRequest $request): View
    // {
    //     $company = Company::create($request->validated());
    //     $readonly = null;
    //     return view('backend.crm.companies.dashboard', compact('company', 'readonly'));
    // }


    // /**
    //  * Handle an incoming request to update an existing company.
    //  *
    //  * @param  \App\Http\Requests\CRM\UpdateCompanyRequest  $request
    //  * @return \Illuminate\Contracts\View\View
    //  */

    // public function update(UpdateCompanyRequest $request): View
    // {
    //     $company = Company::create($request->validated());
    //     $readonly = null;
    //     return view('backend.crm.companies.dashboard', compact('company', 'readonly'));
    // }
}
