<?php
declare(strict_types=1);

namespace App\Modules\Accounting\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\App;
use App\Core\Response;
use App\Modules\Accounting\Models\JournalType;

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

        $types = JournalType::query()
            ->orderBy('code', 'ASC')
            ->get();

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

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

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

        if (JournalType::findByCode($data['code'])) {
            return $this->redirect('/accounting/journal-types')
                ->withError('كود نوع اليومية مستخدم بالفعل');
        }

        JournalType::create([
            'code'              => strtoupper($data['code']),
            'name_ar'           => $data['name_ar'],
            'name_en'           => $data['name_en'],
            'short_name_ar'     => $request->post('short_name_ar'),
            'short_name_en'     => $request->post('short_name_en'),
            'numbering_method'  => $request->post('numbering_method', 'yearly'),
            'number_prefix'     => $request->post('number_prefix'),
            'number_length'     => (int) $request->post('number_length', 6),
            'is_auto_generated' => (int) ($request->post('is_auto_generated', 0)),
            'is_default'        => (int) ($request->post('is_default', 0)),
            'is_active'         => 1,
        ]);

        return $this->redirect('/accounting/journal-types')
            ->withSuccess('تم إنشاء نوع اليومية بنجاح');
    }

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

        $type = JournalType::findOrFail((int) $id);

        return $this->view('Accounting/Views/journal_types/edit', [
            'type' => $type->toArray(),
        ]);
    }

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

        $type = JournalType::findOrFail((int) $id);

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

        $type->update([
            'name_ar'           => $data['name_ar'],
            'name_en'           => $data['name_en'],
            'short_name_ar'     => $request->post('short_name_ar'),
            'short_name_en'     => $request->post('short_name_en'),
            'numbering_method'  => $request->post('numbering_method', 'yearly'),
            'number_prefix'     => $request->post('number_prefix'),
            'number_length'     => (int) $request->post('number_length', 6),
            'is_auto_generated' => (int) ($request->post('is_auto_generated', 0)),
            'is_default'        => (int) ($request->post('is_default', 0)),
            'is_active'         => (int) ($request->post('is_active', 1)),
        ]);

        return $this->redirect('/accounting/journal-types')
            ->withSuccess('تم تحديث نوع اليومية بنجاح');
    }

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

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

        return $this->redirect('/accounting/journal-types')
            ->withSuccess('تم تحديث الحالة بنجاح');
    }
}
