<?php

namespace App\Http\Controllers\Employee;

use App\Http\Controllers\Controller;
use App\Http\Requests\Employee\StatementRequest;
use App\Models\Statement;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class StatementController extends Controller
{
    public function index(): View
    {
        // * Get all statements ordered by statement code ascending
        $statements = Statement::orderBy('statement_code', 'asc')->get();

        return view('employee.pages.statement.index', compact('statements'));
    }

    public function generateStatementCode(): string
    {
        // * Get the latest statement ordered by statement code descending
        $latestStatement = Statement::orderBy('statement_code', 'desc')->first();

        // * If there is no statement yet in the database then the next statement code is T001
        if (!$latestStatement) {
            $nextStatementCode = 'T0001';

            // * If there is a statement, the next statement code will be the latest statement code + 1
        } else {
            $latestStatementCode = $latestStatement->statement_code;
            $number = (int) substr($latestStatementCode, 1);
            $nextNumber = $number + 1;
            $nextStatementCode = 'T' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
        }

        return $nextStatementCode;
    }

    public function create(): View
    {
        // * Generate statement code
        $generateStatementCode = $this->generateStatementCode();

        return view('employee.pages.statement.create', compact('generateStatementCode'));
    }

    public function store(StatementRequest $request): RedirectResponse
    {
        // * Create the statement
        Statement::create([
            'statement_code' => $request->statement_code,
            'statement' => $request->statement,
            'desc' => $request->desc,
        ]);

        // * Notification
        $notification = [
            'message' => 'Statemen telah berhasil ditambahkan',
            'alert-type' => 'info',
        ];

        return redirect()->route('statements')->with($notification);
    }

    public function edit(Statement $statements): View
    {
        return view('employee.pages.statement.edit', compact('statements'));
    }

    public function update(StatementRequest $request, Statement $statements): RedirectResponse
    {
        // * Update the statement
        $statements->update([
            'statement_code' => $request->statement_code,
            'statement' => $request->statement,
            'desc' => $request->desc,
        ]);

        // * Notification
        $notification = [
            'message' => 'Statemen telah berhasil diperbarui',
            'alert-type' => 'info',
        ];

        return redirect()->route('statements')->with($notification);
    }

    public function reorderStatementCode(): void
    {
        // * Get all statement code ordered by statement code ascending
        $statementCode = Statement::orderBy('statement_code', 'asc')->get();

        // * Reorder statement code
        foreach ($statementCode as $key => $statement) {
            $statement->update([
                'statement_code' => 'T' . str_pad($key + 1, 4, '0', STR_PAD_LEFT),
            ]);
        }
    }

    public function destroy(Statement $statements): RedirectResponse
    {
        // * Delete the statement
        $statements->delete();

        // * Reorder statement code
        $this->reorderStatementCode();

        // * Notification
        $notification = [
            'message' => 'Statemen telah berhasil dihapus',
            'alert-type' => 'info',
        ];

        return redirect()->route('statements')->with($notification);
    }
}
