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


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

    def generate_dune(self, module_name: str) -> str:
        """Generate dune file for the bindings library."""
        return f"""; Generated by polyglot-ffi
(library
 (name {module_name}_bindings)
 (public_name {module_name}_bindings)
 (libraries ctypes ctypes.foreign)
 (ctypes
  (external_library_name {module_name})
  (build_flags_resolver
   (vendored (c_flags :standard) (c_library_flags :standard)))
  (headers (include "{module_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)))

(rule
 (targets lib{module_name}_stubs.a dll{module_name}_stubs.so)
 (deps {module_name}_stubs.c {module_name}_stubs.h)
 (action
  (run ocamlmklib -o {module_name}_stubs %{{deps}})))
"""

    def generate_dune_project(self, module_name: str) -> str:
        """Generate dune-project file."""
        return f"""; Generated by polyglot-ffi
(lang dune 3.16)
(using ctypes 0.3)

(name {module_name}_bindings)

(generate_opam_files true)

(package
 (name {module_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))))
"""
