use Mojo::Base -strict;

use Test::More;
use Test::Mojo;
use utils::JWT;
use utils::Email;

# 1. Test JWT Reset Token Generation & Verification
my $dummy_config = {
  jwt_secret => 'test_secret_key_12345',
  jwt_issuer => 'AnchorTest',
};

my $dummy_user = {
  id       => 99,
  email    => 'testuser@example.com',
  password => '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
};

my $token = utils::JWT::generate_reset_token($dummy_config, $dummy_user, 3600);
ok($token, 'Generated password reset token successfully');

my $claims = utils::JWT::verify_reset_token($dummy_config, $token);
ok($claims, 'Verified password reset token successfully');
is($claims->{user_id}, 99, 'Token user_id matches');
is($claims->{email}, 'testuser@example.com', 'Token email matches');
is($claims->{type}, 'pwd_reset', 'Token type is pwd_reset');

# 2. Test utils::Email module (Simulated mode when smtp_host is not provided)
my $email_res = utils::Email::send_forgot_password_email(
  $dummy_config,
  'testuser@example.com',
  'Shubham Sharma',
  'http://localhost:5173/reset-password?token=' . $token,
  60,
  undef
);

ok($email_res->{success}, 'Email helper executed successfully');
is($email_res->{simulated}, 1, 'Email was safely simulated when SMTP is unconfigured');
like($email_res->{link}, qr/reset-password\?token=/, 'Reset link is correctly embedded');

# 3. Test HTML and Plaintext Email Template Generators
my $html = utils::Email::generate_forgot_password_html('Shubham', 'http://localhost:5173/reset-password?token=abc', 60);
like($html, qr/Reset Your Password/, 'HTML template contains title');
like($html, qr/Shubham/, 'HTML template contains user name');
like($html, qr/http:\/\/localhost:5173\/reset-password\?token=abc/, 'HTML template contains custom link');

my $text = utils::Email::generate_forgot_password_text('Shubham', 'http://localhost:5173/reset-password?token=abc', 60);
like($text, qr/Hello Shubham/, 'Text template contains greeting');
like($text, qr/http:\/\/localhost:5173\/reset-password\?token=abc/, 'Text template contains custom link');

# 4. Mojolicious Application Route Tests
my $t = Test::Mojo->new('AnchorBackend');

# Missing email
$t->post_ok('/api/auth/forgot-password' => json => {})
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Missing email parameter');

# Invalid email format
$t->post_ok('/api/auth/forgot-password' => json => { email => 'invalid-email-format' })
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Invalid email address format');

# Test alias route /api/auth/forget-password
$t->post_ok('/api/auth/forget-password' => json => {})
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Missing email parameter');

# Test verify-reset-token missing token
$t->post_ok('/api/auth/verify-reset-token' => json => {})
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Missing password reset token');

# Test verify-reset-token invalid token
$t->post_ok('/api/auth/verify-reset-token' => json => { token => 'invalid.jwt.token' })
  ->status_is(401)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Invalid or expired password reset token');

# Test reset-password missing fields
$t->post_ok('/api/auth/reset-password' => json => {})
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Missing required fields: token, new_password');

# Test reset-password short password
$t->post_ok('/api/auth/reset-password' => json => { token => 'dummy', new_password => '123' })
  ->status_is(400)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'New password must be at least 6 characters long');

# Test reset-password invalid token
$t->post_ok('/api/auth/reset-password' => json => { token => 'invalid.jwt.token', new_password => 'ValidPassword123' })
  ->status_is(401)
  ->json_is('/status' => 'error')
  ->json_is('/message' => 'Invalid or expired password reset token');

done_testing();
