<?php
declare(strict_types=1);

namespace PortalPeople\AuthRedirect\Setup\Patch\Schema;

use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\Patch\DependentPatchInterface;
use Magento\Framework\DB\Ddl\Table;

class CreateLoginAttemptLogTable implements SchemaPatchInterface, PatchRevertableInterface, DependentPatchInterface
{
    private ModuleDataSetupInterface $moduleDataSetup;

    public function __construct(ModuleDataSetupInterface $moduleDataSetup)
    {
        $this->moduleDataSetup = $moduleDataSetup;
    }

    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        if (!$this->moduleDataSetup->getConnection()->isTableExists('portalpeople_authredirect_login_attempt')) {
            $table = $this->moduleDataSetup->getConnection()->newTable(
                $this->moduleDataSetup->getTable('portalpeople_authredirect_login_attempt')
            )->addColumn(
                'attempt_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Attempt ID'
            )->addColumn(
                'email',
                Table::TYPE_TEXT,
                255,
                ['nullable' => true],
                'Attempted Email'
            )->addColumn(
                'ip_address',
                Table::TYPE_TEXT,
                45,
                ['nullable' => false],
                'IP Address'
            )->addColumn(
                'was_successful',
                Table::TYPE_BOOLEAN,
                null,
                ['nullable' => false, 'default' => 0],
                'Was Successful'
            )->addColumn(
                'attempted_at',
                Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
                'Attempt Time'
            )->addIndex(
                'IDX_AUTHREDIRECT_LOGIN_ATTEMPT_EMAIL',
                ['email']
            )->addIndex(
                'IDX_AUTHREDIRECT_LOGIN_ATTEMPT_IP',
                ['ip_address']
            )->addIndex(
                'IDX_AUTHREDIRECT_LOGIN_ATTEMPT_ATTEMPTED_AT',
                ['attempted_at']
            )->addIndex(
                'IDX_AUTHREDIRECT_LOGIN_ATTEMPT_EMAIL_TIME',
                ['email', 'attempted_at']
            )->setComment('AuthRedirect Login Attempt Log');

            $this->moduleDataSetup->getConnection()->createTable($table);
        }

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
        $this->moduleDataSetup->getConnection()->dropTable(
            $this->moduleDataSetup->getTable('portalpeople_authredirect_login_attempt')
        );
        $this->moduleDataSetup->getConnection()->endSetup();
    }

    public static function getDependencies(): array
    {
        return [];
    }

    public function getAliases(): array
    {
        return [];
    }
}
