package com.social.media.controller;

import com.social.media.dto.CompanyDto;
import com.social.media.service.CompanyService;
import com.social.media.domain.shared.PageResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/companies")
public class CompanyController {
    
    @Autowired
    private CompanyService companyService;
    
    @GetMapping
    public ResponseEntity<PageResponse<CompanyDto>> getAllCompanies(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResponse<CompanyDto> companies = companyService.getAllCompanies(page, size);
        return ResponseEntity.ok(companies);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<CompanyDto> getCompanyById(@PathVariable Long id) {
        CompanyDto company = companyService.getCompanyById(id);
        return ResponseEntity.ok(company);
    }
    
    @PostMapping
    public ResponseEntity<CompanyDto> createCompany(@Valid @RequestBody CompanyDto companyDto) {
        CompanyDto createdCompany = companyService.createCompany(companyDto);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdCompany);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<CompanyDto> updateCompany(@PathVariable Long id, @Valid @RequestBody CompanyDto companyDto) {
        CompanyDto updatedCompany = companyService.updateCompany(id, companyDto);
        return ResponseEntity.ok(updatedCompany);
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCompany(@PathVariable Long id) {
        companyService.deleteCompany(id);
        return ResponseEntity.noContent().build();
    }
    
    @GetMapping("/search")
    public ResponseEntity<PageResponse<CompanyDto>> searchCompanies(
            @RequestParam String query,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResponse<CompanyDto> companies = companyService.searchCompanies(query, page, size);
        return ResponseEntity.ok(companies);
    }
    
    @GetMapping("/status/{status}")
    public ResponseEntity<PageResponse<CompanyDto>> getCompaniesByStatus(
            @PathVariable String status,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResponse<CompanyDto> companies = companyService.getCompaniesByStatus(status, page, size);
        return ResponseEntity.ok(companies);
    }
    
    @GetMapping("/plan/{plan}")
    public ResponseEntity<PageResponse<CompanyDto>> getCompaniesByPlan(
            @PathVariable String plan,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResponse<CompanyDto> companies = companyService.getCompaniesByPlan(plan, page, size);
        return ResponseEntity.ok(companies);
    }
    
    @GetMapping("/active")
    public ResponseEntity<PageResponse<CompanyDto>> getActiveCompanies(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageResponse<CompanyDto> companies = companyService.getActiveCompanies(page, size);
        return ResponseEntity.ok(companies);
    }
    
    @GetMapping("/cnpj/{cnpj}")
    public ResponseEntity<CompanyDto> getCompanyByCnpj(@PathVariable String cnpj) {
        CompanyDto company = companyService.getCompanyByCnpj(cnpj);
        if (company != null) {
            return ResponseEntity.ok(company);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/email/{email}")
    public ResponseEntity<CompanyDto> getCompanyByEmail(@PathVariable String email) {
        CompanyDto company = companyService.getCompanyByEmail(email);
        if (company != null) {
            return ResponseEntity.ok(company);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/code/{companyCode}")
    public ResponseEntity<CompanyDto> getCompanyByCompanyCode(@PathVariable String companyCode) {
        CompanyDto company = companyService.getCompanyByCompanyCode(companyCode);
        if (company != null) {
            return ResponseEntity.ok(company);
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/exists/cnpj/{cnpj}")
    public ResponseEntity<Boolean> existsByCnpj(@PathVariable String cnpj) {
        boolean exists = companyService.existsByCnpj(cnpj);
        return ResponseEntity.ok(exists);
    }
    
    @GetMapping("/exists/email/{email}")
    public ResponseEntity<Boolean> existsByEmail(@PathVariable String email) {
        boolean exists = companyService.existsByEmail(email);
        return ResponseEntity.ok(exists);
    }
    
    @GetMapping("/count/active")
    public ResponseEntity<Long> countActiveCompanies() {
        Long count = companyService.countActiveCompanies();
        return ResponseEntity.ok(count);
    }
    
    @GetMapping("/count/plan/{plan}")
    public ResponseEntity<Long> countCompaniesByPlan(@PathVariable String plan) {
        Long count = companyService.countCompaniesByPlan(plan);
        return ResponseEntity.ok(count);
    }
}
