<?php

namespace App\Http\Controllers\Barobill;

use App\Http\Controllers\Controller;
use App\Models\Barobill\BarobillMember;
use App\Models\Tenants\Tenant;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class KakaotalkController extends Controller
{
    /**
     * 카카오톡 메인 (대시보드)
     */
    public function index(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.index'));
        }

        $tenantId = session('selected_tenant_id', 1);
        $currentTenant = Tenant::find($tenantId);
        $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first();

        return view('barobill.kakaotalk.index', compact('currentTenant', 'barobillMember'));
    }

    /**
     * 채널 관리
     */
    public function channels(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.channels'));
        }

        $tenantId = session('selected_tenant_id', 1);
        $currentTenant = Tenant::find($tenantId);
        $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first();

        return view('barobill.kakaotalk.channels.index', compact('currentTenant', 'barobillMember'));
    }

    /**
     * 템플릿 관리
     */
    public function templates(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.templates'));
        }

        $tenantId = session('selected_tenant_id', 1);
        $currentTenant = Tenant::find($tenantId);
        $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first();

        return view('barobill.kakaotalk.templates.index', compact('currentTenant', 'barobillMember'));
    }

    /**
     * 발송 (알림톡/친구톡)
     */
    public function send(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.send'));
        }

        $tenantId = session('selected_tenant_id', 1);
        $currentTenant = Tenant::find($tenantId);
        $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first();

        return view('barobill.kakaotalk.send.index', compact('currentTenant', 'barobillMember'));
    }

    /**
     * 전송내역 조회
     */
    public function history(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.history'));
        }

        $tenantId = session('selected_tenant_id', 1);
        $currentTenant = Tenant::find($tenantId);
        $barobillMember = BarobillMember::where('tenant_id', $tenantId)->first();

        return view('barobill.kakaotalk.history.index', compact('currentTenant', 'barobillMember'));
    }

    /**
     * 사용법 가이드
     */
    public function guide(Request $request): View|Response
    {
        if ($request->header('HX-Request')) {
            return response('', 200)->header('HX-Redirect', route('barobill.kakaotalk.guide'));
        }

        return view('barobill.kakaotalk.guide');
    }
}
