import factory
import random
import time, datetime

from django.db import models

from pairent_app.models import Apartament, User

factory.Faker.override_default_locale('ru_RU');

class OpenID_Address(factory.declarations.BaseDeclaration):
    def evaluate(self, instance, step, extra):
        return ''.join(random.choices(list('abcdef12345678990'), k=6)) + "@vvsu.ru";

class PhoneNumber(factory.declarations.BaseDeclaration):
    def evaluate(self, instance, step, extra):
        return '+799' + str(random.randint(0, 99999999)).zfill(8);

class CSV(factory.declarations.BaseDeclaration):
    def __init__(self, min, max, count_min, count_max):
        self.min = min; self.max = max;
        self.count_min = count_min; self.count_max: count_max;
    
    def evaluate(self, instance, step, extra):
        len_ = random.randint(self.count_min, self.count_max);
        out = [];
        for i in range(len): out.append(random.randint(self.min, self.max));
        return ','.join(out);

class Random(factory.declarations.BaseDeclaration):
    def __init__(self, min, max = None, multiply = 1, list = None):
        if (max == None):
            self.min = 0;
            self.max = min;
        else:
            self.min = min;
            self.max = max;
        self.multiply = multiply;
        self.list = list;
        super().__init__(self);
    
    def evaluate(self, instance, step, extra):
        if (type(self.list) == list):
            return random.choice(self.list);
        
        if (self.multiply != None):
            return random.randint(self.min, self.max) * self.multiply;
        return random.randint(self.min, self.max);

class Date(factory.declarations.BaseDeclaration):

    def __init__(self, min_year = 2022, max_year = datetime.date().year):
        self.min_year = min_year;
        self.max_year = max_year;
        super().__init__(self);

    def evaluate(self, instance, step, extra):
        return datetime.datetime(random.randint(self.min_year, self.max_year), random.randint(1,12), random.randint(1,27)).strftime("%G-%m-%d");

class ApartmentFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = Apartament

    # Base data
    price =                 Random(3,10,5000);
    lastPrice =             Random(3,10,5000);
    bail =                  Random(2,10,1000);

    agencyCommission =      Random(3,5,2500);
    utilitiesPrice =        Random(4,8,1000)
    minimumLeasePeriod =    Random(1,2,6);

    address =               factory.Faker('address', locale='ru_RU');
    description =           factory.Faker('sentence', locale='ru_RU');

    perimetrs =             Random(15,40);
    rooms =                 Random(0,4);
    ceilingHeight =         Random(3,5);
    floorHouse =            Random(2,20);
    floor =                 Random(2,20);
    
    phoneNumber =           PhoneNumber()

    timeToBus =             Random(1, 12, 5);
    timeToTrain =           Random(1, 12, 5);

    # Apartment props
    isFurniture =           Random(0, 1);
    isAnimal =              Random(0, 1);
    isTelevision =          Random(0, 1);
    isChild =               Random(0, 1);
    isInternet =            Random(0, 1);
    isBathroom =            Random(0, 1);
    isRefrigerator =        Random(0, 1);
    isWasher =              Random(0, 1);
    isAirConditioning =     Random(0, 1);
    isFreshRepair =         Random(0, 1);
    
    # House props
    isElevator =            Random(0, 1);
    isParking =             Random(0, 1);
    isGarbageChute =        Random(0, 1);
    isConcierge =           Random(0, 1);
    
    # views = [] # factory.Faker('random_number')

    dateCreate =            Date()

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User
    
    favorites_aparments = CSV(1, 100, 1, 16);
    comparison_apartments = CSV(1, 100, 1, 5);
    psych_test_result = Random(20, 90);
    vvsu_id = OpenID_Address();
    name = factory.faker.Faker('name');
    date_of_birth = Date(1980, 2006);
    about_me = factory.faker.Faker('sentence');
    gender = Random(0,0,0, ['f', 'm', 'n', '?'])

    phone = PhoneNumber()
    email = OpenID_Address()
    telegram = '@uwu'
    discord = '@uwu' # они поменяли формат

    city = Random(0,0,0, ['Владивосток', 'Хабаровск', 'Урюпинск', 'Мухосранск', 'Нью-Йорк'])
    role = 's'