<?php

namespace App\Services\SEO;

use App\Models\Vehicle;
use App\Models\AuctionVehicle;
use App\Models\BlogPost;

class SchemaGenerator
{
    /**
     * Organization & AutoDealer Schema JSON-LD
     */
    public static function organization(): array
    {
        return [
            '@context' => 'https://schema.org',
            '@type' => 'AutoDealer',
            'name' => config('motosolution.brand.name', 'MotoSolution Bangladesh'),
            'alternateName' => 'MotoSolution',
            'url' => config('app.url'),
            'logo' => config('app.url') . '/images/motosolution-logo.png',
            'image' => config('app.url') . '/images/motosolution-showroom.jpg',
            'description' => 'Japanese reconditioned car importer, Niketan Gulshan showroom and Japan live auction pre-order platform in Bangladesh.',
            'telephone' => config('motosolution.contact.phone'),
            'email' => config('motosolution.contact.email'),
            'address' => [
                '@type' => 'PostalAddress',
                'streetAddress' => 'House 24, Road 3, Block B, Niketan, Gulshan-1',
                'addressLocality' => 'Dhaka',
                'postalCode' => '1212',
                'addressCountry' => 'BD',
            ],
            'geo' => [
                '@type' => 'GeoCoordinates',
                'latitude' => '23.7745',
                'longitude' => '90.4182',
            ],
            'openingHoursSpecification' => [
                [
                    '@type' => 'OpeningHoursSpecification',
                    'dayOfWeek' => ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday'],
                    'opens' => '10:00',
                    'closes' => '20:00',
                ],
            ],
            'sameAs' => array_values(config('motosolution.contact.social', [])),
            'priceRange' => '৳৳৳৳',
        ];
    }

    /**
     * Vehicle Schema JSON-LD for Ready Stock vehicles
     */
    public static function vehicle(Vehicle $vehicle): array
    {
        return [
            '@context' => 'https://schema.org',
            '@type' => 'Car',
            'name' => $vehicle->full_name,
            'description' => $vehicle->description ?? "{$vehicle->full_name} Japanese reconditioned vehicle available in Dhaka showroom at MotoSolution Bangladesh.",
            'image' => $vehicle->main_image,
            'vehicleIdentificationNumber' => $vehicle->chassis_number,
            'manufacturer' => [
                '@type' => 'Organization',
                'name' => $vehicle->make,
            ],
            'model' => $vehicle->model,
            'modelDate' => $vehicle->year,
            'vehicleEngine' => [
                '@type' => 'EngineSpecification',
                'engineDisplacement' => [
                    '@type' => 'QuantitativeValue',
                    'value' => $vehicle->engine_cc,
                    'unitCode' => 'CMQ',
                ],
            ],
            'fuelType' => $vehicle->fuel_type,
            'vehicleTransmission' => $vehicle->transmission,
            'driveWheelConfiguration' => $vehicle->drive_type,
            'bodyType' => $vehicle->body_type,
            'color' => $vehicle->color,
            'mileageFromOdometer' => [
                '@type' => 'QuantitativeValue',
                'value' => $vehicle->mileage,
                'unitCode' => 'KMT',
            ],
            'offers' => [
                '@type' => 'Offer',
                'price' => $vehicle->final_price_bdt,
                'priceCurrency' => 'BDT',
                'availability' => $vehicle->status === 'available' ? 'https://schema.org/InStock' : 'https://schema.org/SoldOut',
                'seller' => [
                    '@type' => 'AutoDealer',
                    'name' => 'MotoSolution Bangladesh',
                ],
            ],
        ];
    }

    /**
     * Auction Vehicle Schema JSON-LD
     */
    public static function auctionVehicle(AuctionVehicle $vehicle): array
    {
        return [
            '@context' => 'https://schema.org',
            '@type' => 'Car',
            'name' => $vehicle->full_name,
            'image' => $vehicle->main_image,
            'vehicleIdentificationNumber' => $vehicle->chassis_number,
            'model' => $vehicle->model,
            'modelDate' => $vehicle->year,
            'fuelType' => $vehicle->fuel_type,
            'offers' => [
                '@type' => 'AggregateOffer',
                'lowPrice' => $vehicle->starting_bid_jpy,
                'priceCurrency' => 'JPY',
                'offerCount' => $vehicle->bids_count,
            ],
        ];
    }

    /**
     * BreadcrumbList Schema JSON-LD
     */
    public static function breadcrumbs(array $crumbs): array
    {
        $items = [];
        $i = 1;
        foreach ($crumbs as $name => $url) {
            $items[] = [
                '@type' => 'ListItem',
                'position' => $i++,
                'name' => $name,
                'item' => $url,
            ];
        }

        return [
            '@context' => 'https://schema.org',
            '@type' => 'BreadcrumbList',
            'itemListElement' => $items,
        ];
    }

    /**
     * FAQPage Schema JSON-LD
     */
    public static function faqs(iterable $faqs): array
    {
        $mainEntity = [];
        foreach ($faqs as $faq) {
            $mainEntity[] = [
                '@type' => 'Question',
                'name' => $faq->question,
                'acceptedAnswer' => [
                    '@type' => 'Answer',
                    'text' => $faq->answer,
                ],
            ];
        }

        return [
            '@context' => 'https://schema.org',
            '@type' => 'FAQPage',
            'mainEntity' => $mainEntity,
        ];
    }
}
