pub(crate) use edit_client::edit_client;
pub(crate) use edit_company::edit_company;
pub(crate) use edit_job::edit_job;
pub(crate) use edit_tag::edit_tag;
pub(crate) use edit_worker::edit_worker;

use inquire::{CustomUserError, validator::Validation};

use crate::commands::{Cli, Result};
use clap::{Parser, Subcommand};

mod edit_address;
mod edit_bank;
mod edit_client;
mod edit_company;
mod edit_job;
mod edit_scheme;
mod edit_tag;
mod edit_worker;

use edit_client::EditClient;
use edit_company::EditCompany;
use edit_job::EditJob;
use edit_tag::EditTag;
use edit_worker::EditWorker;

/// Edit database entities
#[derive(Parser)]
pub(crate) struct Edit {
    #[command(subcommand)]
    pub(crate) command: EditCommand,
}

#[derive(Subcommand)]
pub(crate) enum EditCommand {
    Company(EditCompany),
    Worker(EditWorker),
    Client(EditClient),
    Job(EditJob),
    Tag(EditTag),
}

impl Edit {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        match &self.command {
            EditCommand::Company(edit) => edit.run(cli),
            EditCommand::Worker(edit) => edit.run(cli),
            EditCommand::Client(edit) => edit.run(cli),
            EditCommand::Job(edit) => edit.run(cli),
            EditCommand::Tag(edit) => edit.run(cli),
        }
    }
}

pub(crate) fn map_empty(s: String) -> Option<String> {
    if s == "-" { None } else { Some(s) }
}

pub(crate) fn obligatory(input: &str) -> std::result::Result<Validation, CustomUserError> {
    if input.chars().count() == 0 {
        Ok(Validation::Invalid("Cannot be empty!".into()))
    } else {
        Ok(Validation::Valid)
    }
}
