<?php

namespace HelpGent\Database\Migrations\V224;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\Models\Guest as GuestModel;
use HelpGent\App\Models\GuestMeta as GuestMetaModel;
use HelpGent\App\Models\HelpGentUser as HelpGentUserModel;
use HelpGent\App\Models\HelpGentUserMeta as HelpGentUserMetaModel;
use HelpGent\Database\Migrations\Query;

class UserMigration {
    public static function migrate_guest_users( $args = [] ) {
        $page = $args['page'] ?? 1;

        $results = Query::safe_query(
            fn ( $page ) => GuestModel::query()->pagination( $page ), 
            [ $page ], 
            []
        );

        if ( empty( $results ) ) {
            return false;
        }

        foreach ( $results as $item ) {
            $id = HelpGentUserModel::query()->insert_get_id(
                [
                    'email'            => $item->email,
                    'name'             => $item->name,
                    'number'           => $item->number,
                    'company'          => $item->company,
                    'token'            => $item->token,
                    'acceptance'       => $item->acceptance,
                    'token_expires_at' => $item->token_expires_at,
                    'created_at'       => $item->created_at,
                    'updated_at'       => $item->updated_at,
                ]
            );

            if ( $id ) {
                GuestMetaModel::query()->where( 'user_id', $item->id )->insert(
                    [ 
                        'user_id'    => $item->id,
                        'meta_key'   => 'new_user_id',
                        'meta_value' => $id,
                    ] 
                );
            }
        }

        $args['page'] = $page + 1;
        
        return $args;
    }

    public static function migrate_guest_user_meta( $args = [] ): bool {
        $page = $args['page'] ?? 1;

        $results = Query::safe_query(
            fn ( $page ) => GuestMetaModel::query()->where( 'meta_key', '!=', 'new_user_ids' )->pagination( $page ), 
            [ $page ], 
            []
        );

        if ( empty( $results ) ) {
            return false;
        }

        foreach ( $results as $item ) {
            $user_meta_ref = GuestMetaModel::query()->where( 'user_id', $item->user_id )->where( 'meta_key', 'new_user_id' )->first();

            if ( ! $user_meta_ref ) {
                continue;
            }

            HelpGentUserMetaModel::query()->insert(
                [
                    'user_id'    => $user_meta_ref->meta_value,
                    'meta_key'   => $item->meta_key,
                    'meta_value' => $item->meta_value,
                    'created_at' => $item->created_at,
                    'updated_at' => $item->updated_at,
                ]
            );
        }

        return false;
    }

    public static function remove_old_tables(): void {
        Query::safe_query(
            fn () => Query::remove_tables( [ 'helpgent_guest_users', 'helpgent_guest_user_meta' ] ), 
        );
    }
}
