<?php

use Illuminate\Support\Facades\Route;
use App\Enums\UserRole;
use App\Models\Listing;
use App\Models\User;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/og/captain/{username}', function (string $username) {
    $captain = User::where('username', $username)
        ->where('role', UserRole::BOAT_OWNER)
        ->first();

    if (!$captain) {
        return redirect('https://kaptango.com');
    }

    $title = e($captain->name) . ' - KaptanGo';
    $desc = $captain->bio
        ? e(mb_substr(strip_tags($captain->bio), 0, 200))
        : 'KaptanGo\'da kaptanın profilini görüntüle';
    $image = $captain->avatar ? asset('storage/' . $captain->avatar) : '';
    $url = 'https://kaptango.com/' . $captain->username;

    return response()->view('og.captain', compact('title', 'desc', 'image', 'url'));
});

Route::get('/og/listings/{id}', function (int $id) {
    $listing = Listing::with(['coverImage', 'listingType', 'city.province', 'owner:id,name'])->find($id);

    if (!$listing) {
        return redirect('https://kaptango.com');
    }

    $title = e($listing->title);
    $desc = e(mb_substr(strip_tags($listing->description ?? ''), 0, 200));
    $image = $listing->coverImage ? asset('storage/' . $listing->coverImage->path) : '';
    $price = number_format($listing->price ?? 0, 0, ',', '.');
    $url = 'https://kaptango.com/listings/' . $id;

    return response()->view('og.listing', compact('title', 'desc', 'image', 'price', 'url'));
});
