<?php
declare(strict_types=1);

namespace App\Modules\Accounting\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Modules\Accounting\Models\AccountingDimension;
use App\Modules\Accounting\Models\AccountingDimensionValue;

class DimensionController extends Controller
{
    public function index(): Response
    {
        $this->authorize('accounting.dimensions.view');

        $dimensions = AccountingDimension::query()->orderBy('code', 'ASC')->get();

        return $this->view('Accounting/Views/dimensions/index', [
            'dimensions' => $dimensions,
        ]);
    }

    public function store(Request $request): Response
    {
        $this->authorize('accounting.dimensions.manage');

        $data = $this->validate($request->all(), [
            'code'    => 'required',
            'name_ar' => 'required',
            'name_en' => 'required',
        ]);

        AccountingDimension::create([
            'code'        => strtoupper($data['code']),
            'name_ar'     => $data['name_ar'],
            'name_en'     => $data['name_en'],
            'is_required' => (int)($request->post('is_required', 0)),
            'is_active'   => 1,
        ]);

        return $this->redirect('/accounting/dimensions')
            ->withSuccess('تم إنشاء البُعد المحاسبي');
    }

    public function show(Request $request, string $id): Response
    {
        $this->authorize('accounting.dimensions.view');

        $dimension = AccountingDimension::findOrFail((int)$id);
        $values = AccountingDimension::getValuesForDimension((int)$id);

        return $this->view('Accounting/Views/dimensions/show', [
            'dimension' => $dimension->toArray(),
            'values'    => $values,
        ]);
    }

    public function storeValue(Request $request, string $id): Response
    {
        $this->authorize('accounting.dimensions.manage');

        $dimension = AccountingDimension::findOrFail((int)$id);

        $data = $this->validate($request->all(), [
            'code'    => 'required',
            'name_ar' => 'required',
            'name_en' => 'required',
        ]);

        AccountingDimensionValue::create([
            'dimension_id' => (int)$dimension->id,
            'code'         => strtoupper($data['code']),
            'name_ar'      => $data['name_ar'],
            'name_en'      => $data['name_en'],
            'parent_id'    => $request->post('parent_id') ? (int)$request->post('parent_id') : null,
            'is_active'    => 1,
        ]);

        return $this->redirect('/accounting/dimensions/' . $id)
            ->withSuccess('تم إضافة القيمة');
    }

    public function toggleActive(Request $request, string $id): Response
    {
        $this->authorize('accounting.dimensions.manage');

        $dimension = AccountingDimension::findOrFail((int)$id);
        $dimension->update([
            'is_active' => $dimension->is_active ? 0 : 1,
        ]);

        return $this->redirect('/accounting/dimensions')
            ->withSuccess('تم تحديث الحالة');
    }
}
