#!/bin/bash

# Create all remaining microservices
services=("pricing-service:8083" "wallet-service:8084" "notification-service:8085" "media-service:8086" "admin-service:8087" "map-service:8088" "corporate-service:8089" "rating-service:8090")

for service_info in "${services[@]}"; do
    IFS=':' read -r service_name port <<< "$service_info"
    
    echo "Creating $service_name..."
    
    # Create directory structure
    mkdir -p "$service_name/src/main/java/com/camelsoft/wanis/${service_name%-service}"
    mkdir -p "$service_name/src/main/resources"
    
    # Create POM file
    cat > "$service_name/pom.xml" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.camelsoft</groupId>
        <artifactId>beem-microservices</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>$service_name</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <name>$service_name</name>
    <description>${service_name^} for Beem microservices</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.camelsoft</groupId>
            <artifactId>beem-shared</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.38</version>
            <scope>provided</scope>
        </dependency>
        <!-- Swagger/OpenAPI -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>true</executable>
                    <mainClass>com.camelsoft.wanis.${service_name%-service}.${service_name^}Application</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
EOF

    # Create main application class
    service_class_name="${service_name%-service}"
    service_class_name="${service_class_name^}"
    cat > "$service_name/src/main/java/com/camelsoft/wanis/${service_name%-service}/${service_class_name}Application.java" << EOF
package com.camelsoft.wanis.${service_name%-service};

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ${service_class_name}Application {

    public static void main(String[] args) {
        SpringApplication.run(${service_class_name}Application.class, args);
    }
}
EOF

    # Create application.yml
    cat > "$service_name/src/main/resources/application.yml" << EOF
server:
  port: $port

spring:
  application:
    name: $service_name
  cloud:
    config:
      enabled: false
  datasource:
    url: jdbc:h2:mem:${service_name}db
    username: sa
    password: 
    driver-class-name: org.h2.Driver
  jpa:
    hibernate:
      ddl-auto: create-drop
    show-sql: false
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
        format_sql: true
  flyway:
    enabled: false
  h2:
    console:
      enabled: true
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
EOF

    # Create Swagger config
    mkdir -p "$service_name/src/main/java/com/camelsoft/wanis/${service_name%-service}/config"
    cat > "$service_name/src/main/java/com/camelsoft/wanis/${service_name%-service}/config/SwaggerConfig.java" << EOF
package com.camelsoft.wanis.${service_name%-service}.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.camelsoft.wanis.${service_name%-service}.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Beem ${service_class_name} API")
                .description("${service_class_name} for Beem Microservices")
                .version("1.0.0")
                .contact(new Contact("CamelSoft", "https://camelsoft.com", "info@camelsoft.com"))
                .build();
    }
}
EOF

    # Create controller directory
    mkdir -p "$service_name/src/main/java/com/camelsoft/wanis/${service_name%-service}/controller"
    
    echo "Created $service_name"
done

echo "All services created successfully!"
