<?php

namespace App\Http\Controllers\Admin\Notice;

use App\Http\Controllers\Controller;
use App\Services\Admin\Notice\AdminNoticeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;

final class AdminNoticeController extends Controller
{
    public function __construct(
        private readonly AdminNoticeService $service,
    ) {}

    public function index(Request $request)
    {
        $filters = $request->validate([
            'field' => ['nullable', 'string', 'in:subject,content'],
            'q'     => ['nullable', 'string', 'max:200'],
        ]);

        $templates = $this->service->paginate($filters, 15);

        return view('admin.notice.index', [
            'templates' => $templates,   // ✅ blade 호환
            'filters'   => $filters,
        ]);
    }

    public function create()
    {
        return view('admin.notice.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'subject'    => ['required','string','max:80'],
            'content'    => ['required','string'],
            'first_sign' => ['nullable'], // checkbox

            'link_01' => ['nullable','string','max:200'],
            'link_02' => ['nullable','string','max:200'],

            'file_01' => ['nullable','file','mimes:gif,jpg,jpeg,png,hwp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip','max:102400'],
            'file_02' => ['nullable','file','mimes:gif,jpg,jpeg,png,hwp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip','max:102400'],
        ]);

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

        $id = $this->service->create(
            $data,
            $request->file('file_01'),
            $request->file('file_02'),
            $adminId
        );

        return redirect()
            ->route('admin.notice.edit', ['id' => $id])
            ->with('ok', '공지사항이 등록되었습니다.');
    }

    public function edit(int $id, Request $request)
    {
        $row = $this->service->get($id);

        return view('admin.notice.edit', [
            'row' => $row,
            'filters' => $request->query(), // 필요하면 화면에서 "목록" 링크에 그대로 붙여주기용
        ]);
    }

    public function update(int $id, Request $request)
    {
        $data = $request->validate([
            'subject'    => ['required','string','max:80'],
            'content'    => ['required','string'],
            'first_sign' => ['nullable'], // checkbox
            'hiding'     => ['nullable'], // checkbox

            'link_01' => ['nullable','string','max:200'],
            'link_02' => ['nullable','string','max:200'],

            'file_01' => ['nullable','file','mimes:gif,jpg,jpeg,png,hwp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip','max:102400'],
            'file_02' => ['nullable','file','mimes:gif,jpg,jpeg,png,hwp,doc,docx,pdf,ppt,pptx,xls,xlsx,zip','max:102400'],
        ]);

        $this->service->update(
            $id,
            $data,
            $request->file('file_01'),
            $request->file('file_02'),
        );

        return redirect()
            ->route('admin.notice.edit', ['id' => $id] + $request->query()) // ✅ 쿼리 유지
            ->with('ok', '공지사항이 저장되었습니다.');
    }

    public function destroy(int $id, Request $request)
    {
        $this->service->delete($id);

        return redirect()
            ->route('admin.notice.index', $request->query()) // ✅ 쿼리 유지
            ->with('ok', '공지사항이 삭제되었습니다.');
    }

    public function download(int $id, int $slot): StreamedResponse
    {
        $r = $this->service->download($id, $slot);
        abort_unless($r['ok'], 404);

        return Storage::disk('public')->download($r['path'], $r['name']);
    }
}
