<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\DataTables\SubscriptionTypeDataTable;
use App\Models\SubscriptionType;
use App\Http\Requests\Admin\SubscriptionType\StoreSubscriptionTypeRequest;
use App\Http\Requests\Admin\SubscriptionType\UpdateSubscriptionTypeRequest;
use App\Repositories\SubscriptionTypeRepository;
use Illuminate\Http\Request;

class SubscriptionTypeController extends Controller
{
    protected SubscriptionTypeRepository $repository;

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

    public function index(SubscriptionTypeDataTable $dataTable)
    {
        return view('admin.subscription-types.index', [
            'dataTable' => $dataTable->html(),
        ]);
    }

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

    public function create()
    {
        return view('admin.subscription-types.create');
    }

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

    public function show(SubscriptionType $subscriptionType)
    {
        return view('admin.subscription-types.show', [
            'subscriptionType' => $subscriptionType,
        ]);
    }

    public function edit(SubscriptionType $subscriptionType)
    {
        return view('admin.subscription-types.edit', compact('subscriptionType'));
    }

    public function update(UpdateSubscriptionTypeRequest $request, SubscriptionType $subscriptionType)
    {
        $this->repository->update($subscriptionType, $request->validated());
        return response()->json([
            'message' => __('dashboard.subscription_type_updated'),
            'redirect_url' => route('admin.subscription-types.index')
        ]);
    }

    public function destroy(SubscriptionType $subscriptionType)
    {
        $result = $this->repository->delete($subscriptionType, ['contracts']);

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

    public function updateStatus(SubscriptionType $subscriptionType)
    {
        $result = $this->repository->statusToggle($subscriptionType);

        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, ['contracts']);

        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
        ]);
    }
}
