<?php

namespace App\Http\Controllers\Admin\Sms;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\SmsFavorite;
use App\Models\DTO\SearchData;

class FavoriteController extends Controller
{
    private SmsFavorite $smsFavoriteModel;

    public function __construct(SmsFavorite $smsFavorite)
    {
        $this->smsFavoriteModel = $smsFavorite;
    }

    /**
     * SMS > 자주보내는 문자
     * @method GET
     * @see /admin/sms/favorite
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $smsFavoriteData = $this->smsFavoriteModel->data($params);

        if ($smsFavoriteData->rows > 0) {
            $num = listNum($smsFavoriteData->total, $params->page, $params->perPage);
            foreach ($smsFavoriteData->list as $i => $row) {
                $row->num = $num--;
                $row->editURL = route('admin.sms.favorite.edit', $row->id);

                $smsFavoriteData->list[$i] = $row;
            }
        }

        return view('admin.sms.favorite.index', [
            'smsFavoriteData' => $smsFavoriteData,
            'params' => $params
        ]);
    }

    /**
     * 자주 보내는 문자 등록
     * @method GET
     * @see /admin/sms/favorite/create
     */
    public function create()
    {
        return view('admin.sms.favorite.write', [
            'actionURL' => route('admin.sms.favorite.store'),
            'smsFavoriteData' => [],
            'favoriteID' => null
        ]);
    }

    /**
     * 자주 보내는 문자 수정
     * @method GET
     * @see /admin/sms/favorite/{pk}/edit
     */
    public function edit(int $favoriteID)
    {
        return view('admin.sms.favorite.write', [
            'actionURL' => route('admin.sms.favorite.update', $favoriteID),
            'smsFavoriteData' => $this->smsFavoriteModel->find($favoriteID),
            'favoriteID' => $favoriteID
        ]);
    }

    /**
     * 자주 보내는 문자 등록 저장
     * @method POST
     * @see /admin/sms/favorite
     */
    public function store(Request $request)
    {
        $rules = [
            'type' => 'required|numeric|in:1,2',
            'subject' => 'required|string|max:255',
            'content' => 'required|string'
        ];

        $attributes = [
            'type' => '형식',
            'subject' => '제목',
            'content' => '내용'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);

        $this->smsFavoriteModel->insert([
            'type' => $posts['type'],
            'subject' => $posts['subject'],
            'content' => $posts['content'],
            'created_at' => now()
        ]);

        $message = '자주 보내는 문자가 등록되었습니다.';
        return redirect()->route('admin.sms.favorite.index')->with('message', $message);
    }

    /**
     * 자주 보내는 문자 수정 저장
     * @method PUT
     * @see /admin/sms/favorite
     */
    public function update(Request $request)
    {
        $rules = [
            'favorite_id' => 'required|numeric|exists:tb_sms_favorite,id',
            'type' => 'required|numeric|in:1,2',
            'subject' => 'required|string|max:255',
            'content' => 'required|string'
        ];

        $attributes = [
            'favorite_id' => '번호',
            'type' => '형식',
            'subject' => '제목',
            'content' => '내용'
        ];

        $posts = $this->validate($request, $rules, [], $attributes);

        $this->smsFavoriteModel->find($posts['favorite_id'])->update([
            'type' => $posts['type'],
            'subject' => $posts['subject'],
            'content' => $posts['content'],
            'updated_at' => now()
        ]);

        $message = '자주 보내는 문자가 수정되었습니다.';
        return redirect()->route('admin.sms.favorite.edit', $posts['favorite_id'])->with('message', $message);
    }

    /**
     * 자주 보내는 문자 삭제
     * @method DELETE
     * @see /admin/sms/favorite
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $favoriteID) {
                $this->smsFavoriteModel->find($favoriteID)->delete();
            }
        }

        $message = '자주 보내는 문자 정보가 삭제되었습니다.';
        return redirect()->route('admin.sms.favorite.index')->with('message', $message);
    }
}
