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


@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)


@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)


@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)


@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)


@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)



@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)



@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)



@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)



@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)

