"""
Dune build system configuration generator.
"""

from polyglot_ffi.utils.naming import sanitize_module_name


class DuneGenerator:
    """Generate Dune build configuration files."""

    def generate_dune(self, module_name: str) -> str:
        """Generate dune file for the bindings library."""
        safe_name = sanitize_module_name(module_name)
        return f"""; Generated by polyglot-ffi
(library
 (name {safe_name}_bindings)
 (public_name {safe_name}_bindings)
 (libraries ctypes ctypes.foreign)
 (ctypes
  (external_library_name {safe_name})
  (build_flags_resolver
   (vendored (c_flags :standard) (c_library_flags :standard)))
  (headers (preamble "#include \\"{safe_name}_stubs.h\\""))
  (type_description
   (instance Type)
   (functor Type_description))
  (function_description
   (concurrency sequential)
   (instance Function)
   (functor Function_description))
  (generated_types Types_generated)
  (generated_entry_point C)))
"""

    def generate_dune_project(self, module_name: str) -> str:
        """Generate dune-project file."""
        safe_name = sanitize_module_name(module_name)
        return f"""(lang dune 3.16)
(using ctypes 0.3)

; Generated by polyglot-ffi

(name {safe_name}_bindings)

(generate_opam_files true)

(package
 (name {safe_name}_bindings)
 (synopsis "OCaml-Python bindings for {module_name}")
 (description "Auto-generated FFI bindings by polyglot-ffi")
 (depends
  (ocaml (>= 4.14))
  (dune (>= 3.16))
  (ctypes (>= 0.20.0))
  (ctypes-foreign (>= 0.20.0))))
"""
