from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy, reverse
from .models import Cliente, Portafoglio, Attivita, Documento
from .forms import ClienteForm, PortafoglioForm, AttivitaForm, DocumentoForm
from django.shortcuts import get_object_or_404

class ClienteListView(ListView):
    model = Cliente
    template_name = 'core/index.html'
    context_object_name = 'clienti'

class ClienteDetailView(DetailView):
    model = Cliente
    template_name = 'core/article_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        cliente = self.get_object()
        context['portafogli'] = cliente.portafogli.all()
        context['attivita'] = cliente.attivita.all()
        context['documenti'] = cliente.documenti.all()
        return context

class ClienteCreateView(CreateView):
    model = Cliente
    template_name = 'core/cliente_form.html'
    form_class = ClienteForm
    success_url = reverse_lazy('cliente_list')

class ClienteUpdateView(UpdateView):
    model = Cliente
    template_name = 'core/cliente_form.html'
    form_class = ClienteForm
    success_url = reverse_lazy('cliente_list')

class ClienteDeleteView(DeleteView):
    model = Cliente
    template_name = 'core/cliente_confirm_delete.html'
    success_url = reverse_lazy('cliente_list')

# Portafoglio Views
class PortafoglioCreateView(CreateView):
    model = Portafoglio
    form_class = PortafoglioForm
    template_name = 'core/portafoglio_form.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['cliente_pk'] = self.kwargs['cliente_pk']
        return context

    def form_valid(self, form):
        cliente = get_object_or_404(Cliente, pk=self.kwargs['cliente_pk'])
        form.instance.cliente = cliente
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.kwargs['cliente_pk']})

class PortafoglioUpdateView(UpdateView):
    model = Portafoglio
    form_class = PortafoglioForm
    template_name = 'core/portafoglio_form.html'

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})

class PortafoglioDeleteView(DeleteView):
    model = Portafoglio
    template_name = 'core/portafoglio_confirm_delete.html'
    
    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})

# Attivita Views
class AttivitaCreateView(CreateView):
    model = Attivita
    form_class = AttivitaForm
    template_name = 'core/attivita_form.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['cliente_pk'] = self.kwargs['cliente_pk']
        return context

    def form_valid(self, form):
        cliente = get_object_or_404(Cliente, pk=self.kwargs['cliente_pk'])
        form.instance.cliente = cliente
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.kwargs['cliente_pk']})

class AttivitaUpdateView(UpdateView):
    model = Attivita
    form_class = AttivitaForm
    template_name = 'core/attivita_form.html'

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})

class AttivitaDeleteView(DeleteView):
    model = Attivita
    template_name = 'core/attivita_confirm_delete.html'
    
    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})

# Documento Views
class DocumentoCreateView(CreateView):
    model = Documento
    form_class = DocumentoForm
    template_name = 'core/documento_form.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['cliente_pk'] = self.kwargs['cliente_pk']
        return context

    def form_valid(self, form):
        cliente = get_object_or_404(Cliente, pk=self.kwargs['cliente_pk'])
        form.instance.cliente = cliente
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.kwargs['cliente_pk']})

class DocumentoUpdateView(UpdateView):
    model = Documento
    form_class = DocumentoForm
    template_name = 'core/documento_form.html'

    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})

class DocumentoDeleteView(DeleteView):
    model = Documento
    template_name = 'core/documento_confirm_delete.html'
    
    def get_success_url(self):
        return reverse('cliente_detail', kwargs={'pk': self.object.cliente.pk})
