use crate::{
    database::{Database, Error, Result},
    jobber::*,
};

impl JobberDelete for Database {
    fn delete_company(&mut self) -> Result<()> {
        self.company = None;
        Ok(())
    }

    fn delete_worker(&mut self, id: &str, delete: bool) -> Result<()> {
        if let Some(worker) = self.workers.get_mut(id) {
            worker.deleted = delete;
            Ok(())
        } else {
            Err(Error::WorkerNotFound(id.to_string()))
        }
    }

    fn delete_client(&mut self, id: &str, delete: bool) -> Result<()> {
        if let Some(client) = self.clients.get_mut(id) {
            client.deleted = delete;
            Ok(())
        } else {
            Err(Error::ClientNotFound(id.to_string()))
        }
    }

    fn delete_job(&mut self, id: usize, delete: bool) -> Result<()> {
        if let Some(job) = self.jobs.get_mut(id) {
            job.deleted = delete;
            Ok(())
        } else {
            Err(Error::JobNotFound(id))
        }
    }

    fn delete_tag(&mut self, id: &str, delete: bool) -> Result<()> {
        if let Some(tag) = self.tags.get_mut(id) {
            tag.deleted = delete;
            Ok(())
        } else {
            Err(Error::TagNotFound(id.to_string()))
        }
    }

    fn delete_work(&mut self, job_id: usize, work_pos: usize, delete: bool) -> Result<()> {
        if let Some(job) = self.jobs.get_mut(job_id)
            && let Some(work) = job.work.get_mut(work_pos)
        {
            work.deleted = delete;
            Ok(())
        } else {
            Err(Error::WorkNotFound(job_id, work_pos))
        }
    }

    fn delete_invoice(&mut self, no: usize, delete: bool) -> Result<()> {
        if let Some(invoice) = self.invoices.get_mut(no) {
            invoice.deleted = delete;
            Ok(())
        } else {
            Err(Error::InvoiceNotFound(no))
        }
    }
}
