import pkg_resources

from c8connector import C8Connector, ConfigProperty, ConfigAttributeType, Sample, Schema


class MySQLTargetConnector(C8Connector):
    """MySQLTargetConnector's C8Connector impl."""

    def name(self) -> str:
        """Returns the name of the connector."""
        return "MySQL"

    def package_name(self) -> str:
        """Returns the package name of the connector (i.e. PyPi package name)."""
        return "macrometa-target-mysql"

    def version(self) -> str:
        """Returns the version of the connector."""
        return pkg_resources.get_distribution("macrometa_target_mysql").version

    def type(self) -> str:
        """Returns the type of the connector."""
        return "target"

    def description(self) -> str:
        """Returns the description of the connector."""
        return "Send data into a MySQL table."

    def logo(self) -> str:
        """Returns the logo image for the connector."""
        return ""

    def validate(self, integration: dict) -> None:
        """Validate given configurations against the connector.
        If invalid, throw an exception with the cause.
        """
        pass

    def samples(self, integration: dict) -> list[Sample]:
        """Fetch sample data using the given configurations."""
        return []

    def schemas(self, integration: dict) -> list[Schema]:
        """Get supported schemas using the given configurations."""
        return []

    def reserved_keys(self) -> list[str]:
        """List of reserved keys for the connector."""
        return []

    def config(self) -> list[ConfigProperty]:
        """Get configuration parameters for the connector."""
        return [
            ConfigProperty(
                "host",
                "Host",
                ConfigAttributeType.STRING,
                True,
                False,
                description="MySQL host.",
                placeholder_value="mysql_host",
            ),
            ConfigProperty(
                "port",
                "Port",
                ConfigAttributeType.INT,
                True,
                False,
                description="MySQL port.",
                default_value="3306",
            ),
            ConfigProperty(
                "username",
                "Username",
                ConfigAttributeType.STRING,
                True,
                False,
                description="MySQL user.",
                placeholder_value="mysql",
            ),
            ConfigProperty(
                "password",
                "Password",
                ConfigAttributeType.PASSWORD,
                True,
                False,
                description="MySQL password.",
                placeholder_value="password",
            ),
            ConfigProperty(
                "database",
                "Database Name",
                ConfigAttributeType.STRING,
                True,
                True,
                description="MySQL database name.",
                placeholder_value="mysql_db",
            ),
            ConfigProperty(
                "target_table",
                "Target Table",
                ConfigAttributeType.STRING,
                True,
                True,
                description="Destination table name.",
                placeholder_value="mysql_table",
            ),
            ConfigProperty(
                "replace_null",
                "Replace Null",
                ConfigAttributeType.BOOLEAN,
                False,
                False,
                description="Replace null values with default values.",
                default_value="false",
            ),
            ConfigProperty(
                "batch_flush_size",
                "Batch Size",
                ConfigAttributeType.INT,
                False,
                False,
                description="Maximum size of batch. Exceeding this will trigger a batch flush.",
                default_value="10000",
            ),
            ConfigProperty(
                "batch_flush_interval",
                "Batch Flush Interval (Minutes)",
                ConfigAttributeType.INT,
                False,
                False,
                description="Time between batch flush executions.",
                default_value="5",
            ),
            ConfigProperty(
                "lower_case_table_names",
                "Lower Case Tables",
                ConfigAttributeType.BOOLEAN,
                False,
                True,
                description="Use lowercase for table names or not. This will target table name "
                "to lower case",
                default_value="false",
            ),
            ConfigProperty(
                "allow_column_alter",
                "Allow Column Alter",
                ConfigAttributeType.BOOLEAN,
                False,
                True,
                description="Allow column alterations if target table's column type doesn't match "
                "the source data type.",
                default_value="false",
            ),
        ]

    def capabilities(self) -> list[str]:
        """Return the capabilities[1] of the connector."""
        return []
