"""Resource clients for the BeeL SDK."""

from typing import Optional, List
from beel_generated import ApiClient, ApiException
from beel_generated.api import (
    InvoicesApi,
    InvoiceDeliveryApi,
    InvoiceLifecycleApi,
    CustomersApi,
    ProductsApi,
    InvoiceSeriesApi,
    PublicCompaniesApi,
    ConfigurationTaxApi,
    ConfigurationVeriFactuApi,
    ConfigurationPreferencesApi,
)
from beel_generated.models import *


class Invoices:
    """Invoice management operations."""
    
    def __init__(self, api_client: ApiClient):
        self._invoices_api = InvoicesApi(api_client)
        self._delivery_api = InvoiceDeliveryApi(api_client)
        self._lifecycle_api = InvoiceLifecycleApi(api_client)
    
    def list(self, **kwargs):
        """List invoices with optional filters."""
        return self._invoices_api.list_invoices(**kwargs)
    
    def get(self, id: str):
        """Get a single invoice by ID."""
        return self._invoices_api.get_invoice(id)
    
    def create(self, create_invoice_request: CreateInvoiceRequest):
        """Create a new invoice."""
        return self._invoices_api.create_invoice(create_invoice_request)
    
    def update(self, id: str, update_invoice_request: UpdateInvoiceRequest):
        """Update an existing invoice."""
        return self._invoices_api.update_invoice(id, update_invoice_request)
    
    def delete(self, id: str):
        """Delete an invoice."""
        return self._invoices_api.delete_invoice(id)
    
    def issue(self, id: str):
        """Issue an invoice (mark as ready)."""
        return self._lifecycle_api.issue_invoice(id)
    
    def mark_paid(self, id: str, mark_invoice_paid_request: Optional[MarkInvoicePaidRequest] = None):
        """Mark an invoice as paid."""
        if mark_invoice_paid_request is None:
            mark_invoice_paid_request = MarkInvoicePaidRequest()
        return self._lifecycle_api.mark_invoice_paid(id, mark_invoice_paid_request)
    
    def mark_sent(self, id: str, mark_invoice_sent_request: Optional[MarkInvoiceSentRequest] = None):
        """Mark an invoice as sent."""
        if mark_invoice_sent_request is None:
            mark_invoice_sent_request = MarkInvoiceSentRequest()
        return self._lifecycle_api.mark_invoice_sent(id, mark_invoice_sent_request)
    
    def void(self, id: str, void_invoice_request: VoidInvoiceRequest):
        """Void (cancel) an invoice."""
        return self._lifecycle_api.void_invoice(id, void_invoice_request)
    
    def duplicate(self, id: str, duplicate_invoice_request: Optional[DuplicateInvoiceRequest] = None):
        """Duplicate an invoice."""
        if duplicate_invoice_request is None:
            duplicate_invoice_request = DuplicateInvoiceRequest()
        return self._lifecycle_api.duplicate_invoice(id, duplicate_invoice_request)
    
    def schedule(self, id: str, schedule_invoice_request: ScheduleInvoiceRequest):
        """Schedule an invoice for future issuance."""
        return self._lifecycle_api.schedule_invoice(id, schedule_invoice_request)
    
    def reschedule(self, id: str, reschedule_invoice_request: RescheduleInvoiceRequest):
        """Reschedule a scheduled invoice."""
        return self._lifecycle_api.reschedule_invoice(id, reschedule_invoice_request)
    
    def cancel_scheduled(self, id: str):
        """Cancel a scheduled invoice."""
        return self._lifecycle_api.cancel_scheduled_invoice(id)
    
    def download_pdf(self, id: str):
        """Download invoice PDF."""
        return self._delivery_api.download_invoice_pdf(id)
    
    def send_email(self, id: str, send_email_request: Optional[SendEmailRequest] = None):
        """Send invoice by email."""
        if send_email_request is None:
            send_email_request = SendEmailRequest()
        return self._delivery_api.send_invoice_email(id, send_email_request)
    
    def create_corrective(self, create_corrective_invoice_request: CreateCorrectiveInvoiceRequest):
        """Create a corrective (rectification) invoice."""
        return self._invoices_api.create_corrective_invoice(create_corrective_invoice_request)
    
    def submit_verifactu(self, id: str):
        """Submit invoice to VeriFactu."""
        return self._lifecycle_api.submit_invoice_veri_factu(id)
    
    # Bulk operations
    
    def create_bulk(self, create_invoices_bulk_request: CreateInvoicesBulkRequest):
        """Create multiple invoices at once."""
        return self._invoices_api.create_invoices_bulk(create_invoices_bulk_request)
    
    def download_pdf_bulk(self, invoice_ids: List[str]):
        """Download PDFs for multiple invoices."""
        request = DownloadInvoicesPdfBulkRequest(invoice_ids=invoice_ids)
        return self._delivery_api.download_invoices_pdf_bulk(request)
    
    def send_email_bulk(self, send_invoices_bulk_email_request: SendInvoicesBulkEmailRequest):
        """Send emails for multiple invoices."""
        return self._delivery_api.send_invoices_bulk_email(send_invoices_bulk_email_request)
    
    def change_status_bulk(self, change_invoices_status_bulk_request: ChangeInvoicesStatusBulkRequest):
        """Change status for multiple invoices."""
        return self._lifecycle_api.change_invoices_status_bulk(change_invoices_status_bulk_request)


class Customers:
    """Customer management operations."""
    
    def __init__(self, api_client: ApiClient):
        self._api = CustomersApi(api_client)
    
    def list(self, **kwargs):
        """List customers with optional filters."""
        return self._api.list_customers(**kwargs)
    
    def get(self, id: str):
        """Get a single customer by ID."""
        return self._api.get_customer(id)
    
    def create(self, create_customer_request: CreateCustomerRequest):
        """Create a new customer."""
        return self._api.create_customer(create_customer_request)
    
    def update(self, id: str, update_customer_request: UpdateCustomerRequest):
        """Update an existing customer."""
        return self._api.update_customer(id, update_customer_request)
    
    def delete(self, id: str):
        """Delete a customer."""
        return self._api.delete_customer(id)
    
    def deactivate(self, id: str):
        """Deactivate a customer (soft delete)."""
        return self._api.deactivate_customer(id)
    
    def create_bulk(self, create_customers_bulk_request: CreateCustomersBulkRequest):
        """Create multiple customers at once."""
        return self._api.create_customers_bulk(create_customers_bulk_request)
    
    def deactivate_bulk(self, customer_ids: List[str]):
        """Deactivate multiple customers."""
        return self._api.deactivate_customers_bulk(customer_ids)


class Products:
    """Product management operations."""
    
    def __init__(self, api_client: ApiClient):
        self._api = ProductsApi(api_client)
    
    def list(self, **kwargs):
        """List products with optional filters."""
        return self._api.list_products(**kwargs)
    
    def get(self, id: str):
        """Get a single product by ID."""
        return self._api.get_product(id)
    
    def create(self, create_product_request: CreateProductRequest):
        """Create a new product."""
        return self._api.create_product(create_product_request)
    
    def update(self, id: str, update_product_request: UpdateProductRequest):
        """Update an existing product."""
        return self._api.update_product(id, update_product_request)
    
    def delete(self, id: str):
        """Delete a product."""
        return self._api.delete_product(id)
    
    def search(self, query: str):
        """Search products by query string."""
        return self._api.search_products(query)
    
    def create_bulk(self, create_products_bulk_request: CreateProductsBulkRequest):
        """Create multiple products at once."""
        return self._api.create_products_bulk(create_products_bulk_request)
    
    def delete_bulk(self, delete_products_bulk_request: DeleteProductsBulkRequest):
        """Delete multiple products."""
        return self._api.delete_products_bulk(delete_products_bulk_request)


class Series:
    """Invoice series management operations."""
    
    def __init__(self, api_client: ApiClient):
        self._api = InvoiceSeriesApi(api_client)
    
    def list(self):
        """List all invoice series."""
        return self._api.list_series()
    
    def get(self, id: str):
        """Get a single series by ID."""
        return self._api.get_series(id)
    
    def create(self, create_series_request: CreateSeriesRequest):
        """Create a new invoice series."""
        return self._api.create_series(create_series_request)
    
    def update(self, id: str, update_series_request: UpdateSeriesRequest):
        """Update an existing series."""
        return self._api.update_series(id, update_series_request)
    
    def delete(self, id: str):
        """Delete a series."""
        return self._api.delete_series(id)
    
    def set_default(self, id: str):
        """Set a series as default."""
        return self._api.set_default_series(id)


class Companies:
    """Company management operations."""
    
    def __init__(self, api_client: ApiClient):
        self._api = PublicCompaniesApi(api_client)
    
    def list(self):
        """List all companies."""
        return self._api.list_companies()
    
    def get(self, id: str):
        """Get a single company by ID."""
        return self._api.get_company(id)
    
    def create(self, create_company_request: CreateCompanyRequest):
        """Create a new company."""
        return self._api.create_company(create_company_request)
    
    def update(self, id: str, update_company_request: UpdateCompanyRequest):
        """Update an existing company."""
        return self._api.update_company(id, update_company_request)
    
    def delete(self, id: str):
        """Delete a company."""
        return self._api.delete_company(id)


class Config:
    """Configuration operations (tax, VeriFactu, preferences)."""
    
    def __init__(self, api_client: ApiClient):
        self._tax_api = ConfigurationTaxApi(api_client)
        self._verifactu_api = ConfigurationVeriFactuApi(api_client)
        self._preferences_api = ConfigurationPreferencesApi(api_client)
    
    def get_tax_configuration(self):
        """Get current tax configuration."""
        return self._tax_api.get_tax_configuration()
    
    def update_tax_configuration(self, update_tax_configuration_request: UpdateTaxConfigurationRequest):
        """Update tax configuration."""
        return self._tax_api.update_tax_configuration(update_tax_configuration_request)
    
    def get_tax_types(self):
        """Get available tax types."""
        return self._tax_api.get_tax_types()
    
    def get_verifactu_configuration(self):
        """Get VeriFactu configuration."""
        return self._verifactu_api.get_veri_factu_configuration()
    
    def update_language(self, update_language_request: UpdateLanguageRequest):
        """Update user language preference."""
        return self._preferences_api.update_language(update_language_request)
    
    def get_invoice_customization_options(self):
        """Get invoice customization options."""
        return self._preferences_api.get_invoice_customization_options()
