<?php

require __DIR__ . '/vendor/autoload.php';

$app = require_once __DIR__ . '/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

use App\Models\User;
use App\Models\MessageTemplate;

$user = User::first();

if (!$user) {
    echo "No user found. Creating a test user first...\n";
    $user = User::create([
        'name' => 'Test User',
        'phone' => '1234567890',
        'email' => 'testuser@example.com',
        'password' => bcrypt('password123'),
        'is_active' => true,
    ]);
    echo "Created user: {$user->name}\n";
}

$templates = [
    // Auto Reply Templates
    [
        'name' => 'Auto Reply - All Calls',
        'type' => 'auto_reply',
        'call_type' => 'all',
        'content' => 'Hi! Thanks for calling. I am currently unavailable but will get back to you soon. - {name}',
        'priority' => 0,
        'is_active' => true,
        'is_default' => true,
    ],
    [
        'name' => 'Auto Reply - Incoming',
        'type' => 'auto_reply',
        'call_type' => 'incoming',
        'content' => 'Thank you for your call! I am in a meeting right now. Will call you back shortly.',
        'priority' => 1,
        'is_active' => true,
    ],
    [
        'name' => 'Auto Reply - Missed',
        'type' => 'auto_reply',
        'call_type' => 'missed',
        'content' => 'Sorry I missed your call! I will return your call as soon as possible. Thanks for your patience.',
        'priority' => 1,
        'is_active' => true,
    ],
    [
        'name' => 'Auto Reply - Outgoing',
        'type' => 'auto_reply',
        'call_type' => 'outgoing',
        'content' => 'I just tried to reach you. Please call me back when you are free. Thanks!',
        'priority' => 1,
        'is_active' => true,
    ],
    [
        'name' => 'Business Hours Reply',
        'type' => 'auto_reply',
        'call_type' => 'all',
        'content' => 'Our business hours are Mon-Fri 9AM-6PM. Leave a message and we will respond during working hours.',
        'priority' => 5,
        'is_active' => false,
    ],

    // Promotion Templates
    [
        'name' => 'Promo - All Calls',
        'type' => 'promotion',
        'call_type' => 'all',
        'content' => 'Thank you for contacting us! Check out our latest offers at www.example.com - Use code SAVE20 for 20% off!',
        'priority' => 0,
        'is_active' => true,
    ],
    [
        'name' => 'Promo - Incoming Special',
        'type' => 'promotion',
        'call_type' => 'incoming',
        'content' => 'Welcome! As a valued caller, enjoy exclusive 15% discount on your next purchase. Visit shop.example.com',
        'priority' => 1,
        'is_active' => true,
    ],
    [
        'name' => 'Promo - Missed Call Offer',
        'type' => 'promotion',
        'call_type' => 'missed',
        'content' => 'We noticed you tried to reach us! Here is a special offer just for you - FREE shipping on orders over $50!',
        'priority' => 1,
        'is_active' => true,
    ],
    [
        'name' => 'Holiday Promo',
        'type' => 'promotion',
        'call_type' => 'all',
        'content' => 'Happy Holidays! Enjoy 30% off on all products this season. Shop now at holiday.example.com',
        'priority' => 2,
        'is_active' => false,
    ],
];

$created = 0;
$updated = 0;

foreach ($templates as $templateData) {
    $existing = MessageTemplate::where('user_id', $user->id)
        ->where('name', $templateData['name'])
        ->first();

    if ($existing) {
        $existing->update($templateData);
        $updated++;
        echo "Updated: {$templateData['name']}\n";
    } else {
        MessageTemplate::create(array_merge($templateData, ['user_id' => $user->id]));
        $created++;
        echo "Created: {$templateData['name']}\n";
    }
}

echo "\n=== Summary ===\n";
echo "User: {$user->name} (ID: {$user->id})\n";
echo "Created: {$created} templates\n";
echo "Updated: {$updated} templates\n";
echo "Total templates for user: " . MessageTemplate::where('user_id', $user->id)->count() . "\n";
