<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Kreait\Firebase\Contract\Database;
use App\Models\User;
use App\Models\NotificationSetting;

class SendMotherVerifiedPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:send-mother-verified-push {user_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send notification to user after mother document is verified';

    /**
     * Execute the console command.
     */
    public $database;
    public function __construct(Database $database)
    {
        parent::__construct(); 
        $this->database = $database;
    }

    public function handle()
    {
        $user_id = $this->argument('user_id');
        $user = User::find($user_id);
        
        // Note: Using the column name with trailing space if that's how it's defined in DB
        // But for the push sent flag, I created it without a space.
        if($user && $user->{'mother_certificate_verified '} == 1 && $user->is_mother_verified_push_sent == 0){
            send_notification_to_user(
                $this->database,
                $user,
                ['userId' => (string)$user->id],
                NotificationSetting::ALERT,
                'mother_verified_push',
                time(),
                trans('notification.mother_verified_title', [], 'en') . "\n" . trans('notification.mother_verified_title', [], 'ar'),
                trans('notification.mother_verified_message', [], 'en') . "\n" . trans('notification.mother_verified_message', [], 'ar'),
                $user->profile_image_url
            );
            $user->is_mother_verified_push_sent = 1;
            $user->save();
            $this->info('Notification sent successfully to user ID: ' . $user_id);
        } else {
            if (!$user) {
                $this->error('User not found.');
            } elseif ($user->{'mother_certificate_verified '} != 1) {
                $this->error('Mother certificate not verified for this user.');
            } else {
                $this->warn('Notification already sent for this user.');
            }
        }
    }
}
