package pl.polskalokalnie.config;

import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import pl.polskalokalnie.listing.ListingRepository;
import pl.polskalokalnie.listing.ListingStatus;
import pl.polskalokalnie.listing.OfferType;
import pl.polskalokalnie.listing.PropertyListing;
import pl.polskalokalnie.listing.PropertyType;
import pl.polskalokalnie.user.AppUser;
import pl.polskalokalnie.user.AuthProvider;
import pl.polskalokalnie.user.Role;
import pl.polskalokalnie.user.UserRepository;

@Component
public class DataSeeder implements CommandLineRunner {

    private final UserRepository userRepository;
    private final ListingRepository listingRepository;
    private final PasswordEncoder passwordEncoder;
    private final String adminEmail;
    private final String adminPassword;
    private final String adminName;

    public DataSeeder(
            UserRepository userRepository,
            ListingRepository listingRepository,
            PasswordEncoder passwordEncoder,
            @Value("${app.admin.email:admin@mieszko.pl}") String adminEmail,
            @Value("${app.admin.password:Admin123!}") String adminPassword,
            @Value("${app.admin.name:Administrator Mieszko}") String adminName
    ) {
        this.userRepository = userRepository;
        this.listingRepository = listingRepository;
        this.passwordEncoder = passwordEncoder;
        this.adminEmail = adminEmail;
        this.adminPassword = adminPassword;
        this.adminName = adminName;
    }

    @Override
    public void run(String... args) {
        seedAdmin();
        seedDemoListings();
    }

    private void seedAdmin() {
        if (userRepository.existsByEmailIgnoreCase(adminEmail)) {
            return;
        }
        AppUser admin = new AppUser();
        admin.setEmail(adminEmail.toLowerCase());
        admin.setFullName(adminName);
        admin.setPasswordHash(passwordEncoder.encode(adminPassword));
        admin.setRole(Role.ADMIN);
        admin.setProvider(AuthProvider.LOCAL);
        admin.setVerified(true);
        userRepository.save(admin);
    }

    private void seedDemoListings() {
        if (listingRepository.count() > 0) {
            return;
        }
        listingRepository.save(demoListing(
                "Słoneczne 3 pokoje na Mokotowie", "Rozkładowe mieszkanie po remoncie, blisko metra.",
                OfferType.SALE, PropertyType.APARTMENT, "Warszawa", "ul. Puławska 120",
                new BigDecimal("749000"), 62.0, 3, ListingStatus.APPROVED));
        listingRepository.save(demoListing(
                "Dom z ogrodem pod Krakowem", "Wolnostojący dom, działka 600 m², cicha okolica.",
                OfferType.SALE, PropertyType.HOUSE, "Kraków", "ul. Podgórska 8",
                new BigDecimal("1290000"), 145.0, 5, ListingStatus.PENDING));
        listingRepository.save(demoListing(
                "Kawalerka do wynajęcia — Wrocław", "Umeblowana kawalerka w centrum, dostępna od zaraz.",
                OfferType.RENT, PropertyType.APARTMENT, "Wrocław", "ul. Krupnicza 3",
                new BigDecimal("2600"), 30.0, 1, ListingStatus.PENDING));
    }

    private PropertyListing demoListing(
            String title, String description, OfferType offerType, PropertyType propertyType,
            String city, String address, BigDecimal price, Double area, Integer rooms, ListingStatus status
    ) {
        PropertyListing listing = new PropertyListing();
        listing.setTitle(title);
        listing.setDescription(description);
        listing.setOfferType(offerType);
        listing.setPropertyType(propertyType);
        listing.setCity(city);
        listing.setAddress(address);
        listing.setPrice(price);
        listing.setArea(area);
        listing.setRooms(rooms);
        listing.setOwnerEmail("demo.user@mieszko.pl");
        listing.setStatus(status);
        return listing;
    }
}
