<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Product;
use App\Models\Review;

class ReviewSeeder extends Seeder
{
    public function run(): void
    {
        $customer = User::where('email', 'customer@example.com')->first();
        $ani = User::where('email', 'ani@example.com')->first();
        $dedi = User::where('email', 'dedi@example.com')->first();
        $citra = User::where('email', 'citra@example.com')->first();
        $eko = User::where('email', 'eko@example.com')->first();
        $farhan = User::where('email', 'farhan@example.com')->first();

        $kaos = Product::where('sku', 'FSH-KAOS-001')->first();
        $phone = Product::where('sku', 'ELC-PHN-X1')->first();
        $bag = Product::where('sku', 'FSH-BAG-CANVAS')->first();
        $sneakers = Product::where('sku', 'FSH-SHOES-001')->first();
        $blazer = Product::where('sku', 'FSH-BLAZER-001')->first();
        $tv = Product::where('sku', 'ELC-TV-43UHD')->first();
        $earbuds = Product::where('sku', 'ELC-EAR-TWS')->first();
        $blender = Product::where('sku', 'HML-BLN-PORT')->first();
        $lamp = Product::where('sku', 'HML-LMP-SENS')->first();
        $hoodie = Product::where('sku', 'FSH-HOD-ZIP')->first();

        $reviews = [
            [
                'product_id' => $kaos ? $kaos->id : null,
                'user_id' => $customer ? $customer->id : null,
                'rating' => 5,
                'review' => 'Bahan kaosnya adem banget, tebalnya pas dan nyaman dipakai seharian. Recommended!',
            ],
            [
                'product_id' => $kaos ? $kaos->id : null,
                'user_id' => $ani ? $ani->id : null,
                'rating' => 4,
                'review' => 'Bagus sekali kaosnya, warna sesuai gambar. Pas di badan, bahannya adem.',
            ],
            [
                'product_id' => $phone ? $phone->id : null,
                'user_id' => $dedi ? $dedi->id : null,
                'rating' => 5,
                'review' => 'Performa gaming kencang, layar AMOLED 120Hz smooth parah. Kamera 108MP jernih banget.',
            ],
            [
                'product_id' => $phone ? $phone->id : null,
                'user_id' => $citra ? $citra->id : null,
                'rating' => 4,
                'review' => 'Sangat puas dengan layarnya, respon cepat. Baterai agak cepat hangat, tapi charger super cepat.',
            ],
            [
                'product_id' => $bag ? $bag->id : null,
                'user_id' => $ani ? $ani->id : null,
                'rating' => 5,
                'review' => 'Tasnya kokoh, muat laptop 15.6 inch saya dengan aman. Jahitannya rapi dan model vintage keren.',
            ],
            [
                'product_id' => $sneakers ? $sneakers->id : null,
                'user_id' => $customer ? $customer->id : null,
                'rating' => 4,
                'review' => 'Sepatunya ringan dan empuk di kaki. Ukurannya pas sesuai deskripsi, pengiriman cepat.',
            ],
            [
                'product_id' => $blazer ? $blazer->id : null,
                'user_id' => $citra ? $citra->id : null,
                'rating' => 5,
                'review' => 'Potongannya pas banget di badan (slim fit), bahannya adem dan nyaman dipakai kerja.',
            ],
            [
                'product_id' => $tv ? $tv->id : null,
                'user_id' => $eko ? $eko->id : null,
                'rating' => 5,
                'review' => 'Smart TV yang luar biasa. Gambar tajam 4K, suara menggelegar, OS Android lancar tanpa lag.',
            ],
            [
                'product_id' => $earbuds ? $earbuds->id : null,
                'user_id' => $farhan ? $farhan->id : null,
                'rating' => 4,
                'review' => 'Koneksi Bluetooth stabil, bass-nya mantap. Fitur ANC-nya cukup membantu meredam bising.',
            ],
            [
                'product_id' => $earbuds ? $earbuds->id : null,
                'user_id' => $ani ? $ani->id : null,
                'rating' => 5,
                'review' => 'Baterai awet banget, case-nya kecil gampang dibawa. Suara jernih dan detail.',
            ],
            [
                'product_id' => $blender ? $blender->id : null,
                'user_id' => $customer ? $customer->id : null,
                'rating' => 5,
                'review' => 'Sangat praktis buat bikin smoothies di kantor. Sekali charge bisa dipakai berkali-kali.',
            ],
            [
                'product_id' => $lamp ? $lamp->id : null,
                'user_id' => $eko ? $eko->id : null,
                'rating' => 4,
                'review' => 'Sensor cahayanya sensitif. Cahaya redupnya pas untuk tidur dan hemat listrik.',
            ],
            [
                'product_id' => $hoodie ? $hoodie->id : null,
                'user_id' => $farhan ? $farhan->id : null,
                'rating' => 5,
                'review' => 'Bahan katun fleece tebal dan lembut sekali. Jahitan rapi, sleting lancar. Nyaman dipakai.',
            ],
        ];

        foreach ($reviews as $rev) {
            if ($rev['product_id'] && $rev['user_id']) {
                Review::firstOrCreate(
                    [
                        'product_id' => $rev['product_id'],
                        'user_id' => $rev['user_id']
                    ],
                    $rev
                );
            }
        }
    }
}
