<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\DataTables\ContractSettingDataTable;
use App\Models\ContractSetting;
use App\Http\Requests\Admin\ContractSetting\StoreContractSettingRequest;
use App\Http\Requests\Admin\ContractSetting\UpdateContractSettingRequest;
use App\Repositories\ContractSettingRepository;
use Illuminate\Http\Request;

class ContractSettingController extends Controller
{
    protected ContractSettingRepository $repository;

    public function __construct(ContractSettingRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(ContractSettingDataTable $dataTable)
    {
        return view('admin.contract-settings.index', [
            'dataTable' => $dataTable->html(),
        ]);
    }

    public function datatable(ContractSettingDataTable $dataTable)
    {
        return $dataTable->ajax();
    }

    public function create()
    {
        return view('admin.contract-settings.create');
    }

    public function store(StoreContractSettingRequest $request)
    {
        $this->repository->create($request->validated());
        return response()->json([
            'message' => __('dashboard.contract_setting_created'),
            'redirect_url' => route('admin.contract-settings.index')
        ]);
    }

    public function show(ContractSetting $contractSetting)
    {
        return view('admin.contract-settings.show', [
            'contractSetting' => $contractSetting,
        ]);
    }

    public function edit(ContractSetting $contractSetting)
    {
        return view('admin.contract-settings.edit', compact('contractSetting'));
    }

    public function update(UpdateContractSettingRequest $request, ContractSetting $contractSetting)
    {
        $this->repository->update($contractSetting, $request->validated());
        return response()->json([
            'message' => __('dashboard.contract_setting_updated'),
            'redirect_url' => route('admin.contract-settings.index')
        ]);
    }

    public function destroy(ContractSetting $contractSetting)
    {
        $result = $this->repository->delete($contractSetting);

        return response()->json([
            'message' => $result['success'] ? __('dashboard.contract_setting_deleted') : __('dashboard.cannot_delete_has_relations'),
            'success' => $result['success'],
            'relations' => $result['relations']
        ]);
    }

    public function updateStatus(ContractSetting $contractSetting)
    {
        $result = $this->repository->statusToggle($contractSetting);

        return response()->json([
            'message' => $result['message'],
            'success' => $result['success']
        ]);
    }

    public function bulkDelete(Request $request)
    {
        $request->validate(['ids' => 'required|array']);
        $result = $this->repository->bulkDelete($request->ids);

        return response()->json([
            'message' => __('dashboard.deleted_success'),
            'success' => true,
            'data' => $result
        ]);
    }

    public function bulkStatus(Request $request)
    {
        $request->validate(['ids' => 'required|array', 'status' => 'required|boolean']);
        $result = $this->repository->bulkStatus($request->ids, $request->status);

        return response()->json([
            'message' => __('dashboard.status_updated'),
            'success' => true,
            'data' => $result
        ]);
    }
}
