<?php

namespace App\Http\Controllers\Admin\Board\Board;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Models\Board;
use App\Models\BoardMeta;
use Exception;

class NotifyController extends Controller
{
    private Board $boardModel;
    private BoardMeta $boardMetaModel;

    public function __construct(Board $board, BoardMeta $boardMeta)
    {
        $this->boardModel = $board;
        $this->boardMetaModel = $boardMeta;
    }

    /**
     * 게시판 수정 - 알람
     * @method GET
     * @see /admin/board/board/notify/{pk}
     */
    public function show(int $boardID)
    {
        return view('admin.board.board.notify', [
            'boardID' => $boardID,
            'actionURL' => route('admin.board.board.notify.store'),

            // 게시판 정보
            'boardData' => $this->boardModel->find($boardID),

            // 게시판 메타 정보
            'boardMetaData' => $this->boardMetaModel->getAllMeta($boardID),

            // 모든 게시판
            'boardAllData' => $this->boardModel->all()
        ]);
    }

    /**
     * 게시판 수정 - 알람 저장
     * @method POST
     * @see /admin/board/board/notify/{pk}
     */
    public function store(Request $request)
    {
        $rules = [
            'board_id' => 'required|numeric|exists:tb_board,id',
            'send_email_post_super_admin' => 'numeric|in:0,1',
            'send_email_post_post_writer' => 'numeric|in:0,1',
            'send_email_comment_super_admin' => 'numeric|in:0,1',
            'send_email_comment_post_writer' => 'numeric|in:0,1',
            'send_email_comment_comment_writer' => 'numeric|in:0,1',
            'send_email_post_blame_super_admin' => 'numeric|in:0,1',
            'send_email_post_blame_post_writer' => 'numeric|in:0,1',
            'send_email_comment_blame_super_admin' => 'numeric|in:0,1',
            'send_email_comment_blame_post_writer' => 'numeric|in:0,1',
            'send_email_comment_blame_comment_writer' => 'numeric|in:0,1',
            'send_email_post_personal_super_admin' => 'numeric|in:0,1',
            'send_email_post_personal_post_writer' => 'numeric|in:0,1',
            'send_email_post_personal_reply_super_admin' => 'numeric|in:0,1',
            'send_email_post_personal_reply_post_writer' => 'numeric|in:0,1',
            'send_email_post_personal_reply_comment_writer' => 'numeric|in:0,1',

            'send_telegram_post_super_admin' => 'numeric|in:0,1',
            'send_telegram_comment_super_admin' => 'numeric|in:0,1',
            'send_telegram_blame_super_admin' => 'numeric|in:0,1',
            'send_telegram_comment_blame_super_admin' => 'numeric|in:0,1',
            'grp' => 'array',
            'all' => 'array'
        ];

        $attributes = [
            'board_id' => '게시판 PK',
            'send_email_post_super_admin' => '이메일 발송(게시글 작성) - 최고 관리자',
            'send_email_post_post_writer' => '이메일 발송(게시글 작성) - 게시글 작성자',
            'send_email_comment_super_admin' => '이메일 발송(댓글 작성) - 최고 관리자',
            'send_email_comment_post_writer' => '이메일 발송(댓글 작성) - 게시글 작성자',
            'send_email_comment_comment_writer' => '이메일 발송(댓글) - 댓글 작성자',
            'send_email_post_blame_super_admin' => '이메일 발송(게시글 신고) - 최고 관리자',
            'send_email_post_blame_post_writer' => '이메일 발송(게시글 신고) - 게시글 작성자',
            'send_email_comment_blame_super_admin' => '이메일 발송(댓글 신고) - 최고 관리자',
            'send_email_comment_blame_post_writer' => '이메일 발송(댓글 신고) - 게시글 작성자',
            'send_email_comment_blame_comment_writer' => '이메일 발송(댓글 신고) - 댓글 작성자',
            'send_email_post_personal_super_admin' => '이메일 발송(1:1 문의 접수 시) - 최고관리자',
            'send_email_post_personal_post_writer' => '이메일 발송(1:1 문의 접수 시) - 게시글 작성자',
            'send_email_post_personal_reply_super_admin' => '이메일 발송(1:1 문의 답변 시) - 최고관리자',
            'send_email_post_personal_reply_post_writer' => '이메일 발송(1:1 문의 답변 시) - 게시글 작성자',
            'send_email_post_personal_reply_comment_writer' => '이메일 발송(1:1 문의 답변 시) - 댓글 작성자',

            'send_telegram_post_super_admin' => '문자 발송(게시글 작성) - 최고 관리자',
            'send_telegram_comment_super_admin' => '문자 발송(댓글 작성) - 최고 관리자',
            'send_telegram_blame_super_admin' => '문자 발송(게시글 신고) - 최고 관리자',
            'send_telegram_comment_blame_super_admin' => '문자 발송(댓글 신고) - 최고 관리자',
            'grp' => '그룹 적용',
            'all' => '전체 적용'
        ];

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

        // 게시판 정보 조회
        $boardData = $this->boardModel->find($boardID);

        // 메타 정보 저장
        $metaData = [
            // 게시글 작성
            'send_email_post_super_admin' => ($posts['send_email_post_super_admin'] ?? 0),
            'send_email_post_post_writer' => ($posts['send_email_post_post_writer'] ?? 0),

            // 댓글 작성
            'send_email_comment_super_admin' => ($posts['send_email_comment_super_admin'] ?? 0),
            'send_email_comment_post_writer' => ($posts['send_email_comment_post_writer'] ?? 0),
            'send_email_comment_comment_writer' => ($posts['send_email_comment_comment_writer'] ?? 0),

            // 게시글 신고
            'send_email_post_blame_super_admin' => ($posts['send_email_post_blame_super_admin'] ?? 0),
            'send_email_post_blame_post_writer' => ($posts['send_email_post_blame_post_writer'] ?? 0),

            // 댓글 신고
            'send_email_comment_blame_super_admin' => ($posts['send_email_comment_blame_super_admin'] ?? 0),
            'send_email_comment_blame_post_writer' => ($posts['send_email_comment_blame_post_writer'] ?? 0),
            'send_email_comment_blame_comment_writer' => ($posts['send_email_comment_blame_comment_writer'] ?? 0),

            // 1:1 문의
            'send_email_post_personal_super_admin' => ($posts['send_email_post_personal_super_admin'] ?? 0),
            'send_email_post_personal_post_writer' => ($posts['send_email_post_personal_post_writer'] ?? 0),
            'send_email_post_personal_reply_super_admin' => ($posts['send_email_post_personal_reply_super_admin'] ?? 0),
            'send_email_post_personal_reply_post_writer' => ($posts['send_email_post_personal_reply_post_writer'] ?? 0),
            'send_email_post_personal_reply_comment_writer' => ($posts['send_email_post_personal_reply_comment_writer'] ?? 0),

            // 텔레그램
            'send_telegram_post_super_admin' => ($posts['send_telegram_post_super_admin'] ?? 0),
            'send_telegram_comment_super_admin' => ($posts['send_telegram_comment_super_admin'] ?? 0),
            'send_telegram_blame_super_admin' => ($posts['send_telegram_blame_super_admin'] ?? 0),
            'send_telegram_comment_blame_super_admin' => ($posts['send_telegram_comment_blame_super_admin'] ?? 0),
        ];

        DB::beginTransaction();

        try
        {
            $this->boardMetaModel->save($boardID, $metaData);

            /*
             * 그룹, 전체 적용
             */
            $groupData = []; // 그룹적용
            $allData = []; // 전체적용

            $groupKeys = $posts['grp'] ?? [];
            $allKeys = $posts['all'] ?? [];

            if ($groupKeys) {
                $brdGroupData = $this->boardModel->findByGroup($boardData->board_group_id);
                foreach ($brdGroupData as $bKey => $bVal) {
                    if ($bVal->id === $boardID) {
                        continue;
                    }

                    foreach ($groupKeys as $gk => $gv) {
                        // 게시글 작성 시
                        if ($gk === 'send_email_post') {
                            $groupData['send_email_post_super_admin'] = $metaData['send_email_post_super_admin'];
                            $groupData['send_email_post_post_writer'] = $metaData['send_email_post_post_writer'];
                        }
                        // 댓글 작성 시
                        if ($gk === 'send_email_comment') {
                            $groupData['send_email_comment_super_admin'] = $metaData['send_email_comment_super_admin'];
                            $groupData['send_email_comment_post_writer'] = $metaData['send_email_comment_post_writer'];
                            $groupData['send_email_comment_comment_writer'] = $metaData['send_email_comment_comment_writer'];
                        }
                        // 게시글 신고 발생 시
                        if ($gk === 'send_email_post_blame') {
                            $groupData['send_email_post_blame_super_admin'] = $metaData['send_email_post_blame_super_admin'];
                            $groupData['send_email_post_blame_post_writer'] = $metaData['send_email_post_blame_post_writer'];
                        }
                        // 댓글 신고 발생 시
                        if ($gk === 'send_email_comment_blame') {
                            $groupData['send_email_comment_blame_super_admin'] = $metaData['send_email_comment_blame_super_admin'];
                            $groupData['send_email_comment_blame_post_writer'] = $metaData['send_email_comment_blame_post_writer'];
                            $groupData['send_email_comment_blame_comment_writer'] = $metaData['send_email_comment_blame_comment_writer'];
                        }
                        // 1:1 문의 접수 시
                        if ($gk === 'send_email_post_personal') {
                            $groupData['send_email_post_personal_super_admin'] = $metaData['send_email_post_personal_super_admin'];
                            $groupData['send_email_post_personal_post_writer'] = $metaData['send_email_post_personal_post_writer'];
                        }
                        // 1:1 문의 답변 시
                        if ($gk === 'send_email_post_personal_reply') {
                            $groupData['send_email_post_personal_reply_super_admin'] = $metaData['send_email_post_personal_reply_super_admin'];
                            $groupData['send_email_post_personal_reply_post_writer'] = $metaData['send_email_post_personal_reply_post_writer'];
                            $groupData['send_email_post_personal_reply_comment_writer'] = $metaData['send_email_post_personal_reply_comment_writer'];
                        }

                        /*
                         * 텔레그램
                         */
                        // 게시글 작성 시
                        if ($gk === 'send_telegram_post') {
                            $groupData['send_telegram_post_super_admin'] = $metaData['send_telegram_post_super_admin'];
                        }
                        // 댓글 작성 시
                        if ($gk === 'send_telegram_comment') {
                            $groupData['send_telegram_comment_super_admin'] = $metaData['send_telegram_comment_super_admin'];
                        }
                        // 게시글 신고 발생 시
                        if ($gk === 'send_telegram_blame') {
                            $groupData['send_telegram_blame_super_admin'] = $metaData['send_telegram_blame_super_admin'];
                        }
                        // 댓글 신고 발생 시
                        if ($gk === 'send_telegram_comment_blame') {
                            $groupData['send_telegram_comment_blame_super_admin'] = $metaData['send_telegram_comment_blame_super_admin'];
                        }
                    }

                    $this->boardMetaModel->save($bVal->id, $groupData);
                }
            }

            if ($allKeys) {
                $brdAllData = $this->boardModel->all();
                foreach ($brdAllData as $bKey => $bVal) {
                    if ($bVal->id === $boardID) {
                        continue;
                    }
                    foreach ($allKeys as $ak => $av) {
                        // 게시글 작성 시
                        if ($ak === 'send_email_post') {
                            $allData['send_email_post_super_admin'] = $metaData['send_email_post_super_admin'];
                            $allData['send_email_post_post_writer'] = $metaData['send_email_post_post_writer'];
                        }
                        // 댓글 작성 시
                        if ($ak === 'send_email_comment') {
                            $allData['send_email_comment_super_admin'] = $metaData['send_email_comment_super_admin'];
                            $allData['send_email_comment_post_writer'] = $metaData['send_email_comment_post_writer'];
                            $allData['send_email_comment_comment_writer'] = $metaData['send_email_comment_comment_writer'];
                        }
                        // 게시글 신고 발생 시
                        if ($ak === 'send_email_post_blame') {
                            $allData['send_email_post_blame_super_admin'] = $metaData['send_email_post_blame_super_admin'];
                            $allData['send_email_post_blame_post_writer'] = $metaData['send_email_post_blame_post_writer'];
                        }
                        // 댓글 신고 발생 시
                        if ($ak === 'send_email_comment_blame') {
                            $allData['send_email_comment_blame_super_admin'] = $metaData['send_email_comment_blame_super_admin'];
                            $allData['send_email_comment_blame_post_writer'] = $metaData['send_email_comment_blame_post_writer'];
                            $allData['send_email_comment_blame_comment_writer'] = $metaData['send_email_comment_blame_comment_writer'];
                        }
                        // 1:1 문의 접수 시
                        if ($ak === 'send_email_post_personal') {
                            $allData['send_email_post_personal_super_admin'] = $metaData['send_email_post_personal_super_admin'];
                            $allData['send_email_post_personal_post_writer'] = $metaData['send_email_post_personal_post_writer'];
                        }
                        // 1:1 문의 답변 시
                        if ($ak === 'send_email_post_personal_reply') {
                            $allData['send_email_post_personal_reply_super_admin'] = $metaData['send_email_post_personal_reply_super_admin'];
                            $allData['send_email_post_personal_reply_post_writer'] = $metaData['send_email_post_personal_reply_post_writer'];
                            $allData['send_email_post_personal_reply_comment_writer'] = $metaData['send_email_post_personal_reply_comment_writer'];
                        }

                        /*
                         * 텔레그램
                         */
                        // 게시글 작성 시
                        if ($ak === 'send_telegram_post') {
                            $allData['send_telegram_post_super_admin'] = $metaData['send_telegram_post_super_admin'];
                        }
                        // 댓글 작성 시
                        if ($ak === 'send_telegram_comment') {
                            $allData['send_telegram_comment_super_admin'] = $metaData['send_telegram_comment_super_admin'];
                        }
                        // 게시글 신고 발생 시
                        if ($ak === 'send_telegram_blame') {
                            $allData['send_telegram_blame_super_admin'] = $metaData['send_telegram_blame_super_admin'];
                        }
                        // 댓글 신고 발생 시
                        if ($ak === 'send_telegram_comment_blame') {
                            $allData['send_telegram_comment_blame_super_admin'] = $metaData['send_telegram_comment_blame_super_admin'];
                        }
                    }
                    $this->boardMetaModel->save($bVal->id, $allData);
                }
            }

            $message = '게시판 - 알람 정보가 저장되었습니다.';
            DB::commit();
        } catch (Exception $e) {
            $message = $e->getMessage();
            DB::rollBack();
        }

        return redirect()->route('admin.board.board.notify.show', $boardID)->with('message', $message);
    }
}
