<?php

namespace UltraguardPro\modules\loginlimiter\db;

if ( ! defined( 'ABSPATH' ) ) { exit; }

use UltraguardPro\db\BaseTable;

class LoginProtectStatsTable extends BaseTable {

    protected $table_name  = 'ultraguard_login_protect_stats';
    protected $cache_group = 'ultraguard_login_protect_stats';

    public function get_defaults(): array {
        return [
            'id'             => 0,
            'ip_address'     => '',
            'attempted_url'  => '',
            'user_agent'     => '',
            'country_code'   => '',
            'was_redirected' => 0,
            'created_at'     => '',
        ];
    }

    protected function validate_data( $data ) {
        if ( empty( $data['ip_address'] ) || empty( $data['attempted_url'] ) ) {
            return false;
        }

        return $data;
    }

    protected function sanitize_data( $data ): array {
        $out = [];
        foreach ( $data as $key => $value ) {
            if ( 'was_redirected' === $key ) {
                $out[ $key ] = (int) (bool) $value;
            } elseif ( in_array( $key, [ 'attempted_url', 'user_agent' ], true ) ) {
                $out[ $key ] = sanitize_textarea_field( (string) $value );
            } else {
                $out[ $key ] = sanitize_text_field( (string) $value );
            }
        }

        return $out;
    }
}
