<?php

namespace App\Http\Controllers\Backoffice;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\CannedResponse;
use App\Models\ActivityLog;
use Illuminate\Support\Facades\DB;

class CannedResponseController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $responses = CannedResponse::with('creator')
                ->orderBy('id', 'desc')
                ->get();

            return response()->json([
                'data' => $responses->map(function ($response) {
                    return [
                        'id' => $response->id,
                        'content' => $response->content,
                        'created_by_name' => $response->creator ? $response->creator->full_name : '-',
                        'created_at' => $response->created_at ? $response->created_at->format('d.m.Y H:i') : '-',
                    ];
                }),
            ]);
        }

        return view('backoffice.canned-responses.index');
    }

    public function store(Request $request)
    {
        $request->validate([
            'content' => 'required|string|max:255',
        ], [
            'content.required' => 'Cevap metni gereklidir.',
        ]);

        DB::beginTransaction();
        try {
            $response = CannedResponse::create([
                'content' => $request->content,
                'created_by' => auth()->id(),
            ]);

            ActivityLog::log(
                'create_canned_response',
                'canned_responses',
                'Hazir cevap olusturuldu',
                null,
                $response->toArray()
            );

            DB::commit();

            return response()->json([
                'success' => true,
                'message' => 'Hazir cevap basariyla olusturuldu.',
                'response' => $response,
            ]);
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json([
                'success' => false,
                'message' => 'Hazir cevap olusturulurken bir hata olustu: ' . $e->getMessage(),
            ], 500);
        }
    }

    public function show($id)
    {
        $response = CannedResponse::findOrFail($id);

        return response()->json([
            'success' => true,
            'response' => $response,
        ]);
    }

    public function update(Request $request, $id)
    {
        $response = CannedResponse::findOrFail($id);

        $request->validate([
            'content' => 'required|string|max:255',
        ], [
            'content.required' => 'Cevap metni gereklidir.',
        ]);

        DB::beginTransaction();
        try {
            $oldData = $response->toArray();
            $response->update([
                'content' => $request->content,
                'updated_by' => auth()->id(),
            ]);

            ActivityLog::log(
                'update_canned_response',
                'canned_responses',
                'Hazir cevap guncellendi',
                $oldData,
                $response->toArray()
            );

            DB::commit();

            return response()->json([
                'success' => true,
                'message' => 'Hazir cevap basariyla guncellendi.',
                'response' => $response,
            ]);
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json([
                'success' => false,
                'message' => 'Hazir cevap guncellenirken bir hata olustu: ' . $e->getMessage(),
            ], 500);
        }
    }

    public function destroy($id)
    {
        $response = CannedResponse::findOrFail($id);

        DB::beginTransaction();
        try {
            $oldData = $response->toArray();
            $response->delete();

            ActivityLog::log(
                'delete_canned_response',
                'canned_responses',
                'Hazir cevap silindi',
                $oldData,
                null
            );

            DB::commit();

            return response()->json([
                'success' => true,
                'message' => 'Hazir cevap basariyla silindi.',
            ]);
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json([
                'success' => false,
                'message' => 'Hazir cevap silinirken bir hata olustu: ' . $e->getMessage(),
            ], 500);
        }
    }
}
