<?php

namespace App\Http\Controllers\Admin\Qna;

use App\Http\Controllers\Controller;
use App\Services\Admin\Qna\AdminQnaService;
use Illuminate\Http\Request;

final class AdminQnaController extends Controller
{
    public function __construct(
        private readonly AdminQnaService $service
    ) {}

    public function index(Request $request)
    {
        $filters = $request->only([
            'year','enquiry_code','state','q','q_type','date_from','date_to','my_work',
        ]);

        $adminId = (int)auth('admin')->id();

        $data = $this->service->paginate($filters, 20, $adminId);

        return view('admin.qna.index', [
            'rows' => $data['rows'],
            'year' => $data['year'],
            'filters' => $filters,
            'enquiryCodes' => $data['enquiryCodes'],
            'stateLabels' => $data['stateLabels'],
            'categories' => $data['categories'],
            'categoriesMap' => $data['categoriesMap'],
        ]);
    }

    public function show(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        if ($year <= 2000) $year = (int)date('Y');

        $adminId = (int)auth('admin')->id();

        $data = $this->service->detail($seq, $year, $adminId);

        return view('admin.qna.show', [
            'row' => $data['row'],
            'year' => $year,
            'member' => $data['member'],
            'orders' => $data['orders'],
            'stateLabels' => $data['stateLabels'],
            'enquiryCodes' => $data['enquiryCodes'],
            'deferCodes' => $data['deferCodes'],
            'stateLog' => $data['stateLog'],
            'memoLog' => $data['memoLog'],
            'admins' => $data['admins'],
            'actions' => $data['actions'],
            'categories' => $data['categories'],
            'categoriesMap' => $data['categoriesMap'],
        ]);
    }

    public function assignToMe(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $this->service->assignToMe($seq, $year, $adminId);

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '내 업무로 배정했습니다.');
    }

    public function startWork(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $this->service->startWork($seq, $year, $adminId);

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '업무를 시작했습니다.');
    }

    public function returnWork(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $this->service->returnWork($seq, $year, $adminId);

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '업무를 반납했습니다.');
    }

    public function postponeWork(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $validated = $request->validate([
            'defer_code' => ['nullable','string','max:2'],
            'defer_comment' => ['required','string','max:2000'],
        ]);

        $this->service->postponeWork(
            $seq,
            $year,
            $adminId,
            (string)($validated['defer_code'] ?? ''),
            (string)$validated['defer_comment']
        );

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '업무를 보류했습니다.');
    }

    public function saveAnswer(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $validated = $request->validate([
            'answer_content' => ['required','string','max:20000'],
            'answer_sms' => ['required','string','max:500'],
        ]);

        $this->service->saveAnswer(
            $seq,
            $year,
            $adminId,
            (string)$validated['answer_content'],
            (string)$validated['answer_sms'],
        );

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '답변을 저장했습니다.');
    }

    public function completeWork(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $this->service->completeWork($seq, $year, $adminId);

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '업무를 완료 처리했습니다. (요청 방식에 따라 발송)');
    }

    public function addMemo(int $seq, Request $request)
    {
        $year = (int)$request->query('year', date('Y'));
        $adminId = (int)auth('admin')->id();

        $validated = $request->validate([
            'memo' => ['required','string','max:2000'],
        ]);

        $this->service->addMemo($seq, $year, $adminId, (string)$validated['memo']);

        return redirect()
            ->route('admin.qna.show', ['seq'=>$seq, 'year'=>$year] + $request->query())
            ->with('ok', '메모를 추가했습니다.');
    }
}
