# typed: false
require "rails_helper"

RSpec.describe User, type: :model do
  describe "devise modules" do
    it "includes expected devise modules" do
      expect(User.devise_modules).to include(
        :database_authenticatable,
        :registerable,
        :recoverable,
        :rememberable,
        :validatable,
      )
    end
  end

  describe "authentication" do
    let(:password) { "secure_password123" }
    let(:user) { User.create!(email: "test@example.com", password: password) }

    it "creates a user with a password" do
      expect(user).to be_persisted
      expect(user.valid_password?(password)).to be true
    end

    it "fails authentication with incorrect password" do
      expect(user.valid_password?("wrong_password")).to be false
    end

    it "allows updating password" do
      new_password = "new_secure_password456"
      user.update!(password: new_password)

      expect(user.valid_password?(password)).to be false
      expect(user.valid_password?(new_password)).to be true
    end

    it "enforces password complexity" do
      user = User.new(email: "test2@example.com", password: "short")
      expect(user).not_to be_valid
      expect(user.errors[:password]).to be_present
    end
  end

  describe "role enum" do
    it "defines expected roles" do
      expect(User.roles.keys).to match_array(%w[user admin moderator])
    end

    it "defaults to user role" do
      user = User.new
      expect(user.role).to eq("user")
    end
  end

  describe "validations" do
    it { is_expected.to validate_presence_of(:email) }
    it { is_expected.to validate_uniqueness_of(:email).case_insensitive }
    it { is_expected.to validate_presence_of(:password) }
  end
end
