<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\V1\Concerns\RespondsWithApi;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PublicController extends Controller
{
    use RespondsWithApi;

    public function bootstrap()
    {
        return $this->ok([
            'app' => [
                'name' => config('app.name'),
                'api_version' => 'v1',
                'currency' => 'INR',
                'demo_otp_enabled' => (bool) config('app.debug'),
            ],
            'counts' => [
                'cities' => DB::table('cities')->where('status', 'active')->count(),
                'services' => DB::table('services')->where('status', 'active')->count(),
            ],
            'config' => DB::table('app_configs')->pluck('value_json', 'key'),
        ]);
    }

    public function cities()
    {
        return $this->ok(DB::table('cities')->where('status', 'active')->orderBy('name')->get());
    }

    public function localities(int $city)
    {
        return $this->ok(DB::table('localities')->where('city_id', $city)->where('status', 'active')->orderBy('name')->get());
    }

    public function serviceAreaCheck(Request $request)
    {
        $cityId = $request->integer('city_id');
        $localityId = $request->integer('locality_id');

        $query = DB::table('service_areas')->where('status', 'active');

        if ($cityId) {
            $query->where('city_id', $cityId);
        }

        if ($localityId) {
            $query->where(function ($q) use ($localityId) {
                $q->whereNull('locality_id')->orWhere('locality_id', $localityId);
            });
        }

        $area = $query->first();

        return $this->ok([
            'serviceable' => (bool) $area,
            'area' => $area,
            'message' => $area ? 'Service available.' : 'Service not available yet.',
        ]);
    }

    public function categories()
    {
        return $this->ok(DB::table('service_categories')->where('status', 'active')->orderBy('sort_order')->get());
    }

    public function services(Request $request)
    {
        $query = DB::table('services')
            ->join('service_categories', 'service_categories.id', '=', 'services.category_id')
            ->where('services.status', 'active')
            ->select(
                'services.*',
                'service_categories.name as category_name',
                DB::raw('(select min(base_price) from service_prices where service_prices.service_id = services.id) as starting_price')
            )
            ->orderBy('services.name');

        if ($request->filled('category_id')) {
            $query->where('services.category_id', $request->integer('category_id'));
        }

        return $this->ok($query->get());
    }

    public function service(string $slug)
    {
        $service = DB::table('services')->where('slug', $slug)->first();

        if (! $service) {
            return $this->fail('SERVICE_NOT_FOUND', 'Service not found.', [], 404);
        }

        return $this->ok([
            'service' => $service,
            'variants' => DB::table('service_variants')->where('service_id', $service->id)->where('status', 'active')->get(),
            'addons' => DB::table('service_addons')->where('service_id', $service->id)->where('status', 'active')->get(),
            'checklist' => DB::table('service_checklist_items')->where('service_id', $service->id)->orderBy('sort_order')->get(),
        ]);
    }

    public function cmsPage(string $slug)
    {
        $page = DB::table('cms_pages')->where('slug', $slug)->where('status', 'published')->first();

        return $page ? $this->ok($page) : $this->fail('PAGE_NOT_FOUND', 'CMS page not found.', [], 404);
    }

    public function faqs()
    {
        return $this->ok(DB::table('faqs')->where('status', 'active')->orderBy('sort_order')->get());
    }

    public function banners()
    {
        return $this->ok(DB::table('app_banners')->where('status', 'active')->orderBy('sort_order')->get());
    }

    public function onboarding()
    {
        return $this->ok(DB::table('onboarding_screens')->where('status', 'active')->orderBy('sort_order')->get());
    }

    public function languages(string $locale)
    {
        return $this->ok(DB::table('language_strings')->where('locale', $locale)->get());
    }
}
