<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\User;
use App\Models\Market\UserInfo;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as SymfonyCommand;
use Psr\Container\ContainerInterface;

/**
 * @Command
 */
class ImportReadmeMemberData extends SymfonyCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('import:readme-member');
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('Import member data from README.md for Rex Moran Loba');
    }

    public function handle()
    {
        $email = 'rexm.loba@gmail.com';
        $user = User::where('email', $email)->first();

        if (!$user) {
            $this->output->info("User with email {$email} not found. Creating...");
            $user = User::create([
                'name' => 'Rex Moran Loba',
                'fullname' => 'Rex Moran Loba',
                'email' => $email,
                'username' => 'rexmloba',
                'password' => password_hash('password', PASSWORD_DEFAULT),
                'hashkey' => bin2hex(random_bytes(16)),
                'active' => true,
            ]);
        }

        $userInfo = $user->userInfo;
        if (!$userInfo) {
            $userInfo = new UserInfo(['user_id' => $user->id]);
            $userInfo->hashkey = bin2hex(random_bytes(16));
        }

        $userInfo->firstname = 'Rex Moran';
        $userInfo->lastname = 'Loba';
        $userInfo->email = $email;
        $userInfo->is_active = true;
        
        // Example address data based on feedback
        $userInfo->addresses = [
            [
                'house_no' => 'Unit 1234',
                'street' => 'Street Name',
                'barangay' => 'Barangay Name',
                'city' => 'City Name',
                'province' => 'Province Name',
                'region' => 'Region Name',
                'country' => 'Philippines',
                'zipcode' => '1234'
            ]
        ];

        if ($userInfo->save()) {
            $this->output->success("Profile for Rex Moran Loba updated successfully.");
        } else {
            $this->output->error("Failed to update profile.");
        }
    }
}
