<?php

namespace App\Console\Commands;

use App\Models\Recurring;
use App\Models\Sale;
use App\Models\User;
use Illuminate\Console\Command;

class DataMigration extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'data:migration';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command is created for only system data migration';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
//        $this->info('Start updateLastProcessDate');
//        $this->updateLastProcessDate();
//        $this->info('End updateLastProcessDate');
//
//
//        $this->info('Start updateUserAccessEndDateDate');
//        $this->updateUserAccessEndDateDate();
//        $this->info('End updateUserAccessEndDateDate');
    }

    // the function is for updating recurrings table last_process_date by sale created_at date
    private function updateLastProcessDate()
    {
        $recurring_users = (new Recurring())->getRecurrings();

        $logData['action'] = 'UPDATE_LAST_PROCESS_DATE';

        if (!empty($recurring_users)) {

            foreach ($recurring_users as $recurring_user) {

                $sale_data = (new Sale())->getLatestSalesByUserId($recurring_user['user_id']);

                if (!empty($sale_data)) {

                    $inputData['last_process_date'] = $sale_data['created_at'];
                    $result = $recurring_user->update($inputData);

                    // log data prepared
                    $logData['result'][] = [
                        'sale_data' => $sale_data,
                        'updated' => $result,
                        'user_id' => $recurring_user['user_id'],
                        'data' => $inputData
                    ];
                }
            }
            appLog($logData);
        }
    }

    // the function is for updating users table user_access_end_date by sale created_at date
    private function updateUserAccessEndDateDate()
    {
        $users = (new User())->getAllClients();

        $logData['action'] = 'UPDATE_USER_ACCESS_END_DATE';

        if (!empty($users)) {

            foreach ($users as $user) {

                if (!empty($user->last_payment_date)) {

                    $inputData['user_access_end_date'] = getDateAfterSpicificDay($user->last_payment_date, config('constant.PAYMENT_INTERVAL_DAY'));
                    $result = $user->update($inputData);

                    // log data prepared
                    $logData['result'][] = [
                        'updated' => $result,
                        'user_id' => $user->id,
                        'added_date' => config('constant.PAYMENT_INTERVAL_DAY'),
                        'data' => $inputData
                    ];
                }
            }
            appLog($logData);
        }
    }


}
