from django.shortcuts import render, redirect, get_object_or_404
from osinacore.models import *
from django.contrib.auth.decorators import login_required
from django.urls import reverse
from django.http import HttpResponseRedirect
from osinacore.decorators import *
from customercore.models import *
from support.models import *

@staff_login_required
def delete_customer_modal(request, customer_id):
    customer = get_object_or_404(CustomerProfile, id=customer_id)

    if request.method == 'POST':
        customer.delete()
        return redirect('customers')

    context = {
        'customer': customer,
    }
    return render(request, "delete_templates/delete-customer-modal.html", context)


@staff_login_required
def delete_business_modal(request, business_id):
    business = get_object_or_404(Business, id=business_id)

    if request.method == 'POST':
        business.delete()
        return redirect('businesses')

    context = {
        'business': business,
    }
    return render(request, "delete_templates/delete-business-modal.html", context)

@staff_login_required
def delete_staff_modal(request, staff_id):
    staff = get_object_or_404(StaffProfile, id=staff_id)

    if request.method == 'POST':
        staff.delete()
        return redirect('users')

    context = {
        'staff': staff,
    }
    return render(request, "delete_templates/delete-staff-modal.html", context)


@staff_login_required
def delete_project_modal(request, project_id):
    project = get_object_or_404(Project, id=project_id)

    if request.method == 'POST':
        project.delete()
        return redirect('my-projects')

    context = {
        'project': project,
    }
    return render(request, "delete_templates/delete-project-modal.html", context)


@staff_login_required
def delete_project_note_modal(request, note_id):
    note = get_object_or_404(Note, id=note_id)

    if request.method == 'POST':
        project_id = note.project_id
        project = get_object_or_404(Project, id=project_id)
        note.delete()

        return redirect('detailed-project', project_id=project.project_id)

    context = {
        'note': note,
    }
    
    return render(request, "delete_templates/delete-project-note-modal.html", context)



@staff_login_required
def delete_task_modal(request, task_id):
    task = get_object_or_404(Task, id=task_id)

    if request.method == 'POST':
        task.delete()
        return redirect('my-tasks')

    context = {
        'task': task,
    }
    return render(request, "delete_templates/delete-task-modal.html", context)


@staff_login_required
def delete_ticket_modal(request, ticket_id):
    ticket = get_object_or_404(Ticket, id=ticket_id)

    if request.method == 'POST':
        ticket.delete()
        return redirect('tickets')

    context = {
        'ticket': ticket,
    }
    return render(request, "delete_templates/delete-ticket-modal.html", context)



@staff_login_required
def delete_point_modal(request, point_id, task_id):
    task = get_object_or_404(Task, id=task_id)
    point = get_object_or_404(Point, id=point_id)

    if request.method == 'POST':
        point.delete()

    task_id_str = task.task_id
    
    showpoints_url = reverse('showpoints', args=[task_id_str])
    return HttpResponseRedirect(showpoints_url)



@staff_login_required
def delete_task_point_modal(request, point_id, task_id):
    task = get_object_or_404(Task, id=task_id)
    point = get_object_or_404(Point, id=point_id)

    if request.method == 'POST':
        point.delete()

    task_id_str = task.task_id
    
    task_details_url = reverse('detailed-task', args=[task_id_str])
    return HttpResponseRedirect(task_details_url)



@staff_login_required
def delete_note_modal(request, note_id):
    note = get_object_or_404(Note, id=note_id)

    if request.method == 'POST':
        note.delete()
        return redirect('my-notes')

    context = {
        'note': note,
    }
    
    return render(request, "delete_templates/delete-note-modal.html", context)


@staff_login_required
def delete_payment_modal(request):

    context = {
    
    }
    
    return render(request, "delete_templates/delete-payment-modal.html", context)


@staff_login_required
def delete_staff_position_modal(request):

    context = {
    
    }
    
    return render(request, "delete_templates/delete-staff-position-modal.html", context)



@staff_login_required
def delete_utility_modal(request, utility_type, utility_id):
    if request.method == 'POST':
        if utility_type == 'department':
            department = get_object_or_404(Department, id=utility_id)
            department.delete() 
            return redirect('departments')

        if utility_type == 'project_type':
            project_type = get_object_or_404(ProjectType, id=utility_id)
            project_type.delete() 
            return redirect('projecttypes')
        
        if utility_type == 'job_position':
            job_position = get_object_or_404(JobPosition, id=utility_id)
            job_position.delete() 
            return redirect('jobpositions')
        
        if utility_type == 'business_type':
            business_type = get_object_or_404(BusinessType, id=utility_id)
            business_type.delete() 
            return redirect('businesstypes')
        
        if utility_type == 'reference':
            reference = get_object_or_404(Reference, id=utility_id)
            reference.delete() 
            return redirect('references')
        
        if utility_type == 'tag':
            tags = get_object_or_404(Tag, id=utility_id)
            tags.delete() 
            return redirect('tags')

    context = {
        'utility_type': utility_type,
        'utility_id': utility_id,
    }
    return render(request, "delete_templates/delete-utility-modal.html", context)
