package com.petshop.backend.config;

import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    public DataInitializer(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        this.userRepository = userRepository;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public void run(String... args) {
        System.out.println("==== DataInitializer: Starting user creation ====");

        User admin = userRepository.findByUsername("admin").orElse(null);
        if (admin == null) {
            System.out.println("Creating admin user...");
            admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setEmail("admin@petshop.com");
            admin.setFirstName("Admin");
            admin.setLastName("User");
            admin.setFullName("Admin User");
            admin.setPhone("000-000-1000");
            admin.setRole(User.Role.ADMIN);
            admin.setActive(true);
            userRepository.save(admin);
            System.out.println("Admin user created successfully");
        } else {
            System.out.println("Admin user already exists");
            boolean updated = false;
            if (admin.getFirstName() == null || admin.getFirstName().isEmpty()) {
                admin.setFirstName("Admin");
                updated = true;
            }
            if (admin.getLastName() == null || admin.getLastName().isEmpty()) {
                admin.setLastName("User");
                updated = true;
            }
            if (admin.getFullName() == null || admin.getFullName().isEmpty()) {
                admin.setFullName("Admin User");
                updated = true;
            }
            if (admin.getEmail() == null || admin.getEmail().isEmpty()) {
                admin.setEmail("admin@petshop.com");
                updated = true;
            }
            if (admin.getPhone() == null || admin.getPhone().isEmpty()) {
                admin.setPhone("000-000-1000");
                updated = true;
            }
            if (admin.getActive() == null) {
                admin.setActive(true);
                updated = true;
            }
            if (admin.getRole() == null) {
                admin.setRole(User.Role.ADMIN);
                updated = true;
            }
            if (updated) {
                userRepository.save(admin);
                System.out.println("Admin user normalized");
            }
        }

        User staff = userRepository.findByUsername("staff").orElse(null);
        if (staff == null) {
            System.out.println("Creating staff user...");
            staff = new User();
            staff.setUsername("staff");
            staff.setPassword(passwordEncoder.encode("staff123"));
            staff.setEmail("staff@petshop.com");
            staff.setFirstName("Staff");
            staff.setLastName("User");
            staff.setFullName("Staff User");
            staff.setPhone("000-000-1001");
            staff.setRole(User.Role.STAFF);
            staff.setActive(true);
            userRepository.save(staff);
            System.out.println("Staff user created successfully");
        } else {
            System.out.println("Staff user already exists");
            boolean updated = false;
            if (staff.getFirstName() == null || staff.getFirstName().isEmpty()) {
                staff.setFirstName("Staff");
                updated = true;
            }
            if (staff.getLastName() == null || staff.getLastName().isEmpty()) {
                staff.setLastName("User");
                updated = true;
            }
            if (staff.getFullName() == null || staff.getFullName().isEmpty()) {
                staff.setFullName("Staff User");
                updated = true;
            }
            if (staff.getEmail() == null || staff.getEmail().isEmpty()) {
                staff.setEmail("staff@petshop.com");
                updated = true;
            }
            if (staff.getPhone() == null || staff.getPhone().isEmpty()) {
                staff.setPhone("000-000-1001");
                updated = true;
            }
            if (staff.getActive() == null) {
                staff.setActive(true);
                updated = true;
            }
            if (staff.getRole() == null) {
                staff.setRole(User.Role.STAFF);
                updated = true;
            }
            if (updated) {
                userRepository.save(staff);
                System.out.println("Staff user normalized");
            }
        }

        User customer = userRepository.findByUsername("customer").orElse(null);
        if (customer == null) {
            System.out.println("Creating customer user...");
            customer = new User();
            customer.setUsername("customer");
            customer.setPassword(passwordEncoder.encode("customer123"));
            customer.setEmail("customer@petshop.com");
            customer.setFirstName("Test");
            customer.setLastName("Customer");
            customer.setFullName("Test Customer");
            customer.setPhone("000-000-1002");
            customer.setRole(User.Role.CUSTOMER);
            customer.setActive(true);
            userRepository.save(customer);
            System.out.println("Customer user created successfully");
        } else {
            System.out.println("Customer user already exists");
            boolean updated = false;
            if (customer.getFirstName() == null || customer.getFirstName().isEmpty()) {
                customer.setFirstName("Test");
                updated = true;
            }
            if (customer.getLastName() == null || customer.getLastName().isEmpty()) {
                customer.setLastName("Customer");
                updated = true;
            }
            if (customer.getFullName() == null || customer.getFullName().isEmpty()) {
                customer.setFullName("Test Customer");
                updated = true;
            }
            if (customer.getEmail() == null || customer.getEmail().isEmpty()) {
                customer.setEmail("customer@petshop.com");
                updated = true;
            }
            if (customer.getPhone() == null || customer.getPhone().isEmpty()) {
                customer.setPhone("000-000-1002");
                updated = true;
            }
            if (customer.getActive() == null) {
                customer.setActive(true);
                updated = true;
            }
            if (customer.getRole() == null) {
                customer.setRole(User.Role.CUSTOMER);
                updated = true;
            }
            if (updated) {
                userRepository.save(customer);
                System.out.println("Customer user normalized");
            }
        }

        System.out.println("==== DataInitializer: Completed ====");
    }
}
