from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.decorators import login_required
from .forms import SignUpForm, CompanyForm, TenderForm, BidForm
from .models import Company, Membership, Tender, Bid

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

@login_required
def dashboard(request):
    memberships = Membership.objects.filter(user=request.user)
    companies = [m.company for m in memberships]
    context = {
        'companies': companies
    }
    return render(request, "core/dashboard.html", context)

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('dashboard')
    else:
        form = SignUpForm()
    return render(request, 'core/signup.html', {'form': form})

def logout_view(request):
    logout(request)
    return redirect('home')

@login_required
def create_company(request):
    if request.method == 'POST':
        form = CompanyForm(request.POST)
        if form.is_valid():
            company = form.save()
            Membership.objects.create(user=request.user, company=company, role='owner')
            return redirect('dashboard')
    else:
        form = CompanyForm()
    return render(request, 'core/create_company.html', {'form': form})

@login_required
def tender_list(request, company_id):
    company = get_object_or_404(Company, pk=company_id)
    tenders = Tender.objects.filter(company=company)
    context = {
        'company': company,
        'tenders': tenders
    }
    return render(request, 'core/tender_list.html', context)

@login_required
def tender_detail(request, tender_id):
    tender = get_object_or_404(Tender, pk=tender_id)
    bids = Bid.objects.filter(tender=tender)
    context = {
        'tender': tender,
        'bids': bids
    }
    return render(request, 'core/tender_detail.html', context)

@login_required
def create_tender(request, company_id):
    company = get_object_or_404(Company, pk=company_id)
    if request.method == 'POST':
        form = TenderForm(request.POST)
        if form.is_valid():
            tender = form.save(commit=False)
            tender.company = company
            tender.save()
            return redirect('tender_list', company_id=company.id)
    else:
        form = TenderForm()
    context = {
        'form': form,
        'company': company
    }
    return render(request, 'core/create_tender.html', context)

@login_required
def create_bid(request, tender_id):
    tender = get_object_or_404(Tender, pk=tender_id)
    membership = Membership.objects.filter(user=request.user).first()
    if not membership:
        # Handle case where user has no company
        return redirect('dashboard') # Or show an error
    
    company = membership.company

    if request.method == 'POST':
        form = BidForm(request.POST)
        if form.is_valid():
            bid = form.save(commit=False)
            bid.tender = tender
            bid.company = company
            bid.save()
            return redirect('tender_detail', tender_id=tender.id)
    else:
        form = BidForm()
    
    context = {
        'form': form,
        'tender': tender
    }
    return render(request, 'core/create_bid.html', context)

