<?php

namespace Database\Seeders;

use App\Models\Contact;
use App\Models\Conversation;
use App\Models\Intent;
use App\Models\IntentPhrase;
use App\Models\Organization;
use App\Models\ResponseTemplate;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $user = User::query()->updateOrCreate([
            'email' => env('ADMIN_EMAIL', 'admin@vejalnai.local'),
        ], [
            'name' => env('ADMIN_NAME', 'VejalNai Admin'),
            'password' => env('ADMIN_PASSWORD', 'password'),
        ]);

        $organization = Organization::query()->updateOrCreate([
            'slug' => env('ADMIN_ORGANIZATION_SLUG', 'vejalnai'),
        ], [
            'name' => env('ADMIN_ORGANIZATION_NAME', 'VejalNai'),
        ]);

        if ($organization->widget_public_key === null) {
            $organization->forceFill([
                'widget_public_key' => env('ADMIN_WIDGET_PUBLIC_KEY', 'wpk_'.Str::random(32)),
                'widget_enabled' => true,
            ])->save();
        }

        $organization->memberships()->updateOrCreate([
            'user_id' => $user->id,
        ], [
            'role' => 'owner',
        ]);

        $deliveryIntent = Intent::query()->updateOrCreate([
            'organization_id' => $organization->id,
            'slug' => 'delivery-charge',
        ], [
            'name' => 'Delivery Charge',
            'description' => 'Answers delivery charge questions.',
            'status' => 'active',
            'confidence_threshold' => 0.7000,
        ]);

        IntentPhrase::query()->updateOrCreate([
            'organization_id' => $organization->id,
            'intent_id' => $deliveryIntent->id,
            'phrase' => 'delivery charge koto',
        ], [
            'locale' => 'bn',
            'status' => 'approved',
        ]);

        ResponseTemplate::query()->updateOrCreate([
            'organization_id' => $organization->id,
            'intent_id' => $deliveryIntent->id,
            'locale' => 'bn',
            'priority' => 10,
        ], [
            'status' => 'approved',
            'body' => 'Dhakar vitore delivery charge 80 taka.',
        ]);

        $contact = Contact::query()->firstOrCreate([
            'organization_id' => $organization->id,
            'channel' => 'messenger',
            'external_id' => 'demo-customer',
        ], [
            'name' => 'Demo Customer',
            'locale' => 'bn',
            'last_seen_at' => now(),
        ]);

        Conversation::query()->firstOrCreate([
            'organization_id' => $organization->id,
            'contact_id' => $contact->id,
            'channel' => 'messenger',
        ], [
            'status' => 'open',
            'last_message_at' => now(),
        ]);
    }
}
