<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Client;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ClientController extends Controller
{
    public function index(): View
    {
        $page_title = 'Müşteriler';
        $page_description = 'Çok müşterili reklam raporlama müşteri listesi';
        $clients = Client::latest()->paginate(20);

        return view('admin.clients.index', compact('page_title', 'page_description', 'clients'));
    }

    public function create(): View
    {
        $page_title = 'Yeni Müşteri';
        $page_description = 'Yeni müşteri kaydı oluştur';
        $client = new Client([
            'status' => 'active',
            'currency_code' => 'TRY',
            'timezone' => 'Europe/Istanbul',
            'report_language' => 'tr',
        ]);

        return view('admin.clients.create', compact('page_title', 'page_description', 'client'));
    }

    public function store(Request $request): RedirectResponse
    {
        $client = Client::create($this->validated($request));

        return redirect()->route('admin.clients.show', $client)->with('success', 'Müşteri oluşturuldu.');
    }

    public function show(Client $client): View
    {
        $page_title = $client->company_name;
        $page_description = 'Müşteri detay ve bağlantı özeti';
        $client->load(['platformConnections', 'reportSchedules', 'generatedReports']);

        return view('admin.clients.show', compact('page_title', 'page_description', 'client'));
    }

    public function edit(Client $client): View
    {
        $page_title = 'Müşteri Düzenle';
        $page_description = $client->company_name;

        return view('admin.clients.edit', compact('page_title', 'page_description', 'client'));
    }

    public function update(Request $request, Client $client): RedirectResponse
    {
        $client->update($this->validated($request));

        return redirect()->route('admin.clients.show', $client)->with('success', 'Müşteri güncellendi.');
    }

    public function destroy(Client $client): RedirectResponse
    {
        $client->delete();

        return redirect()->route('admin.clients.index')->with('success', 'Müşteri silindi.');
    }

    private function validated(Request $request): array
    {
        $data = $request->validate([
            'company_name' => ['required', 'string', 'max:255'],
            'contact_person' => ['nullable', 'string', 'max:255'],
            'email' => ['nullable', 'email', 'max:255'],
            'phone' => ['nullable', 'string', 'max:50'],
            'website' => ['nullable', 'string', 'max:255'],
            'sector' => ['nullable', 'string', 'max:120'],
            'status' => ['required', 'in:active,passive,paused'],
            'currency_code' => ['required', 'string', 'size:3'],
            'timezone' => ['required', 'string', 'max:80'],
            'report_language' => ['required', 'in:tr,en'],
            'primary_color' => ['nullable', 'string', 'max:20'],
            'report_recipients_raw' => ['nullable', 'string'],
            'notes' => ['nullable', 'string'],
            'internal_notes' => ['nullable', 'string'],
        ]);

        $data['report_recipients'] = collect(preg_split('/\r\n|\r|\n|,/', $data['report_recipients_raw'] ?? ''))
            ->map(fn ($mail) => trim($mail))
            ->filter()
            ->values()
            ->all();

        unset($data['report_recipients_raw']);

        return $data;
    }
}
