from django.shortcuts import render, redirect
from django.contrib.auth import login as auth_login, authenticate
from .forms import CompanySignUpForm, InfluencerSignUpForm, LoginForm

def index(request):
    return render(request, 'core/index.html')

def pricing(request):
    return render(request, 'core/pricing.html')

def login(request):
    if request.method == 'POST':
        form = LoginForm(request, data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                auth_login(request, user)
                return redirect('dashboard')
    else:
        form = LoginForm()
    return render(request, 'core/login.html', {'form': form})

def signup(request):
    if request.method == 'POST':
        if request.POST.get('role') == 'company':
            form = CompanySignUpForm(request.POST)
        else:
            form = InfluencerSignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
            return redirect('dashboard')
    else:
        form = InfluencerSignUpForm()
    return render(request, 'core/signup.html', {'form': form})

def dashboard(request):
    return render(request, 'core/dashboard.html')